Learn R Programming

rmongodb (version 1.8.0)

mongo.aggregation: Aggregation pipeline

Description

Aggregation pipeline

Usage

mongo.aggregation(mongo, ns, pipeline, explain = NULL, allowDiskUse = NULL, cursor = NULL, ...)

Arguments

mongo
(mongo) A mongo connection object.
ns
(string) The namespace of the collection in which to find distinct keys.
pipeline
(list of mongo.bson objects) representing aggregation query pipeline. Alternately, pipeline may be a list of list which will be converted to a mongo.bson list object by mongo.bson.from.list().

Alternately, pipeline may be a list of valid JSON character strings which will be converted to a mongo.bson object by mongo.bson.from.JSON().

explain
(logical) Optional, MongoDB 2.6+. Specifies to return the information on the processing of the pipeline. References above.
allowDiskUse
(logical) Optional, MongoDB 2.6+. Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in the dbPath directory.
cursor
(mongo.bson) Optional, MongoDB 2.6+. Specify a document that contains options that control the creation of the cursor object.
...
Arguments to be passed to methods, such as mongo.bson.to.list, fromJSON Unfortunately, current underlying mongo-c-driver can return BSON from aggreagation camand. Cursors are not supported.

Alternately, cursor may be a list which will be converted to a mongo.bson object by mongo.bson.from.list().

Alternately, cursor may be a valid JSON character string which will be converted to mongo.bson object by mongo.bson.from.JSON().

Value

NULL if the command failed. mongo.get.err() may be MONGO_COMMAND_FAILED.mongo.bson The result of aggregation.

Details

See http://docs.mongodb.org/manual/reference/command/aggregate/ http://docs.mongodb.org/manual/core/aggregation-pipeline/.

See Also

mongo.command, mongo.simple.command, mongo.find, mongo.

Examples

Run this code
# using the zips example data set
mongo <- mongo.create()
# insert some example data
data(zips)
colnames(zips)[5] <- "orig_id"
ziplist <- list()
ziplist <- apply( zips, 1, function(x) c( ziplist, x ) )
res <- lapply( ziplist, function(x) mongo.bson.from.list(x) )
if (mongo.is.connected(mongo)) {
    mongo.insert.batch(mongo, "test.zips", res )
    pipe_1 <- mongo.bson.from.JSON('{"$group":{"_id":"$state", "totalPop":{"$sum":"$pop"}}}')
    cmd_list <- list(pipe_1)
    res <- mongo.aggregation(mongo, "test.zips", cmd_list)
}
mongo.destroy(mongo)

Run the code above in your browser using DataLab