Aggregation and Grouping in MongoDB

Lets try to under how MongoDB's aggregation system works.

Setting up the senario

You have a list of movies. Each movie object has numerous properties. Title, release date, cast, director etc.

Potentially every movie can have comments associated with it. However, the comments are stored in a different collection and referenced to the movie using the movie's ID.

Each comment has its own properties.

  1. _id //auto generated by MongoDB
  2. email //of the poster
  3. text //the actual comment
  4. date //the date it was posted on

We have a ton of movies and a ton of comments and we want to make sense of the data a little bit.

Lets say we want to find the emails of the users who post the most often. Say the top 20 most frequent commenters.

Aggregate

This looks like a job for MongoDB's aggregate method. The best analogy for it, presented by the MongoDB team, is that the data is put through a converyer belt. Where at stages, some action is performed on the date to condense it, to make it more legible.

In this case we will be performing a couple of these actions on the data.

Group

The first action is we will group the data:

{
  $group: {
    _id: "$email",
    count: {
      $sum: 1
    }
  }
}

Think of these stages as creating their own documents. Each document must have an _id. In this case we have specified it as the email of the commenter. This is a required field.

Then we must add a field that will hold the <accumulator_n>: <expression_n>.

In our case its count (which we made up) and we are using the accumulator $sum, you have a wide varierty of choices.

And the expression would mean 'adding one document at at time'.

Sort

We want the information to be returned with the the highest count first

{ $sort { count: -1  } } // descending order

Limit

Finally we can limit the output to just 20 items

{  $limit { 20 } }

Output

We get a list of 20 commenters and the count of how many comments they made.

Aggregation is MongoDB's way of condensing information down to bite sized pieces. Lots to learn so lets get to it!