vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

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

NameDescription
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 = 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]]);
}

Example

The same example, but using an array of arguments with custom options

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]]);

	AggregateOptions options;
	options.cursor.batchSize = 10; // pre-fetch the first 10 results
	auto results = db["coll"].aggregate(args, options);
}

Authors

Sönke Ludwig

Copyright

© 2012-2016 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.