1

Other commands work correctly for me, such as:

let commandDictionary = ["count": "trials"]
let error: NSError?
let result = myConnection.runCommandWithDictionary(
    commandDictionary, 
    onDatabaseName: databaseName, 
    error: &error
)

Attempting a distinct command doesn't, though:

let commandDictionary = ["distinct": "trials", "key": "location"]
let error: NSError?
let result = myConnection.runCommandWithDictionary(
    commandDictionary, 
    onDatabaseName: databaseName, 
    error: &error
)

After running this, the value of error is: Optional("MONGO_COMMAND_FAILED: The command returned with \'ok\' value of 0"). Running what (I believe) is the same command in the shell gives me sane values:

db.runCommand({distinct: "trials", key: "location"})

Update #1:

Also, MongoConnection.serverVersion() gives me: "2.6.3", and MongoConnection.serverError() returns nil. The verbose server logs show this when running the command in the shell:

2014-12-12T10:11:54.556-0500 [conn69] command eim.$cmd command: distinct { distinct: "trials", key: "metadata.location" } keyUpdates:0 numYields:0 locks(micros) r:606 reslen:210 0ms

And they show this for the command as run by ObjCMongoDb:

2014-12-12T10:10:25.945-0500 [conn171] command eim.$cmd command: isMaster { key: "metadata.location", distinct: "trials" } ntoreturn:1 keyUpdates:0 numYields:0 reslen:138 0ms

Update #2:

It seems that somewhere the distinct command is getting mapped to the isMaster command. This happens for other commands. For instance, the command {listDatabases:1} is mapped to isMaster, as well. However, the command {buildInfo:1} is correctly mapped to buildInfo.

GarlicFries
  • 8,095
  • 5
  • 36
  • 53
  • Seems like you're doing it right. MongoConnection+Diagnostics has a `-serverVersion` method. What does it return? – paulmelnikow Dec 12 '14 at 14:35
  • Is what you're printing there the value of `error`? Does `-[connection serverError]` give you anything useful? – paulmelnikow Dec 12 '14 at 14:39
  • Yes, that's the value of `error`. Will edit to make that clear. – GarlicFries Dec 12 '14 at 15:13
  • `MongoConnection.serverVersion()` gives me: `"2.6.3"`, and `MongoConnection.serverError()` returns `nil` every time. – GarlicFries Dec 12 '14 at 15:17
  • Strange. I can't reproduce it, I haven't tried in Swift but I have equivalent Objective-C code that works fine. Are you using replication? Have you tried it in Objective-C? – paulmelnikow Dec 12 '14 at 17:38

1 Answers1

0

As discussed in this issue report, the problem is that the database requires the keys to be in order, but Swift, like Objective-C, doesn't preserve the order of dictionary keys.

As author of the library, my suggested workaround is to construct the command using OrderedDictionary, which is included with the library.

Update:

In version 0.12.0 of the library, -runCommandWithDictionary is deprecated. Here's code for the new method:

let error: NSError?
let result = myConnection.runCommandWithName(
    "distinct", 
    value: "trials",
    arguments: ["key": "location"],
    onDatabaseName: databaseName, 
    error: &error
)
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114