MongoCollection.aggregate

Calculates aggregate values for the data in a collection.

struct MongoCollection
Bson
aggregate
(
ARGS...
)
()

Parameters

pipeline ARGS

A sequence of data aggregation processes. These can either be given as separate parameters, or as a single array parameter.

Return Value

Type: Bson

An array of documents returned by the pipeline

Throws

Exception if a DB communication error occured

Examples

Example taken from the MongoDB documentation

import vibe.db.mongo.mongo;

void test() {
	auto db = connectMongoDB("127.0.0.1").getDatabase("test");
	auto results = db["coll"].aggregate(
		["$match": ["status": "A"]],
		["$group": ["_id": Bson("$cust_id"),
			"total": Bson(["$sum": Bson("$amount")])]],
		["$sort": ["total": -1]]);
}

The same example, but using an array of arguments

import vibe.db.mongo.mongo;

void test() {
	auto db = connectMongoDB("127.0.0.1").getDatabase("test");

	Bson[] args;
	args ~= serializeToBson(["$match": ["status": "A"]]);
	args ~= serializeToBson(["$group": ["_id": Bson("$cust_id"),
			"total": Bson(["$sum": Bson("$amount")])]]);
	args ~= serializeToBson(["$sort": ["total": -1]]);

	auto results = db["coll"].aggregate(args);
}

See Also

Meta