I am trying to access the mongodb aggregation structure from rmongodb. It should be accessible through mongo.command (), but I cannot configure bson correctly.
Here is an example that can be reproduced. In R, set up a test collection:
mongo <- mongo.create() db <- "test" ns <- "test.people" buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "John") mongo.bson.buffer.append(buf, "age", 22L) b <- mongo.bson.from.buffer(buf) mongo.insert(mongo, ns, b); buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "John") mongo.bson.buffer.append(buf, "age", 35L) b <- mongo.bson.from.buffer(buf) mongo.insert(mongo, ns, b); buf <- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "Fred") mongo.bson.buffer.append(buf, "age", 27L) b <- mongo.bson.from.buffer(buf) mongo.insert(mongo, ns, b); Now, on the mongo console, the following works: use test db.runCommand({ aggregate : "people", pipeline : [ { $group : { _id : '$name', total : { $sum : 1 } } } ] })
It seems that the following in R will achieve the same effect:
mongo.command(mongo, "test", list( aggregate="people", pipeline=list( group=list( "_id"="$name", total=list("$sum"=1) ) ) ))
This does not work.
For this particular aggregation, I know that there are other ways to do this. I just provide it as a simple example, but in the future I want to move on to more complex aggregates.
Also, I know of another mongodb R package that I might consider to do the trick, but I have invested quite a lot in using this package right now.