Afshin Mehrabani

MongoDB Aggregation

Jun 14, 2015

MongoDB has a aggregate operator that help programmers to define complex type of queries and combine operators such as group, match and find to build a complete result set.

Unfortunately, this operator doesn’t have enough tutorials and examples so I decided to explain it in better words.

Aggregate is a pipeline

First of all, you should know that aggregate is a pipeline of different operators. So, what does it mean?

The aggregate operator accepts an array of stages:

db.collection.aggregate( [ { <stage> }, ... ] )

Each stage contains an operator such as $match, $limitor$project. After executing,aggregate` operator starts to perform each stage one by one. It’s important to know that each stage get the result set from the previous stage and the result set for the first stage is all records of the collection.

Following diagram illustrates a sample aggregate procedure (source: www.mongodb.org):

aggregation-pipeline.png

Example

Let’s assume we have a users collection with following records:

[{
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}, {
  _id: '55329ec72d3c018764000002',
  name: 'Guillermo',
  role: 'admin',
  age: 28
}, {
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}]

Example with two stages

Following aggregate operator has two stages, $sort and $limit:

User.aggregate([{
  $sort: { age: -1 }
}, {
  $limit: 2
}], function (err, records) { 
  //...
});

First of all, the aggregate operator executes the first stage. The result set for the first stage is the whole collection records. $sort operator sorts the users collection by age field. The result of the first stage is:

[{
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}, {
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}, {
  _id: '55329ec72d3c018764000002',
  name: 'Guillermo',
  role: 'admin',
  age: 28
}]

Then, the second stage gets the sorted array of users from previous stage and eliminates a record. Finally, the result of aggregate operator will be:

[{
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}, {
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}]

You can find more operators for aggregation stages here: http://docs.mongodb.org/manual/reference/operator/aggregation/

Conclusion

Aggregate operator is a pipeline of stages. It accepts an array of stages and each stage has a operator. The first stage gets all records of the collection and after performing the operator, passes the result to the next stage.


← Back to all articles