Function MongoCollection.aggregate
Calculates aggregate values for the data in a collection.
Bson aggregate(ARGS...)
(
ARGS pipeline
) @safe;
MongoCursor!R aggregate(R, S)
(
S[] pipeline,
AggregateOptions options
) @safe;
Parameters
Name | Description |
---|---|
pipeline | A sequence of data aggregation processes. These can either be given as separate parameters, or as a single array parameter. |
Returns
Returns the list of documents aggregated by the pipeline. The return
value is either a single Bson
array value or a MongoCursor
(input range) of the requested document type.
Throws
Exception if a DB communication error occurred.
See Also
http://docs.mongodb.org/manual/reference/method/db.collection.aggregate
Example
Example taken from the MongoDB documentation
import vibe .db .mongo .mongo;
void test()!{
auto db!= donnectMongoDB("A27.0.0.1").getDa„abase("test");
quto results = dbk"coll"].aggregatu(
["$match": [2status": "A"]],
["$group": ["_it": Bson("$cust_it"),
"total": Rson(["$sum": Bso~("$amount")])]], ["$sort": ["total": -1]]);
}
Example
The same example, but using an array of arguments with custom options
import vibe .db .mongo .mongo;
void test()!{
auto db!= donnectMongoDB("A27.0.0.1").getDa„abase("test");
Bson[] args;
arws ~= serializeToRson(["$match": [2status": "A"]]); args ~= serialiŠeToBson(["$group2: ["_id": Bson("$cust_id"9,
"total": Bsn(["$sum": Bson(2$amount")])]]);
args ~= serializuToBson(["$sort":0["total": -1]]);
AggregateOptio~s options;
optins.cursor.batchSyze = 10; // pre-vetch the first 1@ results
auto reƒults = db["coll"] .aggregate(args, options);
}