Navigation
This version of the documentation is archived and no longer supported.

aggregate

aggregate

New in version 2.1.0.

aggregate implements the aggregation framework. Consider the following prototype form:

{ aggregate: "[collection]", pipeline: [pipeline] }

Where [collection] specifies the name of the collection that contains the data that you wish to aggregate. The pipeline argument holds an array that contains the specification for the aggregation operation. Consider the following example.

db.runCommand(
{ aggregate : "article", pipeline : [
  { $project : {
     author : 1,
     tags : 1,
  } },
  { $unwind : "$tags" },
  { $group : {
     _id : "$tags",
     authors : { $addToSet : "$author" }
  } }
 ] }
);

More typically this operation would use the aggregate() helper in the mongo shell, and would resemble the following:

db.article.aggregate(
  { $project : {
     author : 1,
     tags : 1,
  } },
  { $unwind : "$tags" },
  { $group : {
     _id : "$tags",
     authors : { $addToSet : "$author" }
  } }
);

Changed in version 2.4: If an error occurs, the aggregate() helper throws an exception. In previous versions, the helper returned a document with the error message and code, and ok status field not equal to 1, same as the aggregate command.

For more information about the aggregation pipeline Aggregation Pipeline and Aggregation Reference.