Question

I am using the ObjCMongoDB library to back a simple iPad application. I understand the basic CRUD operations as well as use of -[MongoConnection runCommandWithDictionary:onDatabaseName:error:] to execute database commands such as findAndModify.

With respect to the Aggregation Framework or just aggregation in general, it looks like I can execute simple database commands like count in the following manner:

NSDictionary *commandDict = @{@"count": @"myCollection"}:
[myMongoConnection runCommandWithDictionary:commandDict onDatabaseName:@"myDB" error:&err];

Is it possible to execute more complex commands/queries like the examples here? I tried the following to no avail.

NSDictionary *commandDict = @{@"aggregate":@"myCollection", @"query": @{@"group":@"myDocType"}};

Am still learning both Obj-C as well as MongoDB so apologies if this is an uninformed question.

Was it helpful?

Solution

Your syntax for the aggregate command appears to be incorrect.

See this example from the docs for aggregate:

db.runCommand(
{ aggregate : "article", pipeline : [
  { $project : {
     author : 1,
     tags : 1,
  } },
  { $unwind : "$tags" },
  { $group : {
     _id : "$tags",
     authors : { $addToSet : "$author" }
  } }
 ] }
);

If you run the Mongo shell from the command line, you can test your command directly right there, before writing it in Objective-C.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top