By Spencer Norman

Recently I had to query our MongoDB to find out which of our users had the most likes, dislikes and matches for some internal tracking. This was initially difficult to figure out, but helped me understand the MongoDB aggregation framework much better. It might help (but isn’t completely necessary) to read through the docs on the aggregation framework before looking through this tutorial. Note:The aggregation framework was introduced in MongoDB 2.2

Say you have a MongoDB collection that looks like this:

And you have many users, each having many likes and dislikes. Note: These likes or dislikes could be other users or could be any other document or collection within your DB, what these _id’s represent doesn’t really matter, you can use the aggregation framework to quickly compile statistics based on them.

Get the total number of likes

We needed to get the total count of the number likes among all users. We can do this using the aggregation framework like this:

Ok, so what are we doing here.

  • We $unwind our array. Unwind “Peels off the elements of an array individually, and returns a stream of documents. $unwind returns one document for every member of the unwound array within every source document.” according to the docs. Read more about $unwind
  • We group by a _id: null. Essentially we are creating a group that will contain all of our likes. If you don’t understand this part, it may make more sense with our next example.
  • We then add a new key “number” to our output and it’s value is equal to the $sum of the number of documents in the group (because of the null _id, this equals all the documents)
  • Your output from this aggregation command will be something like this:
    { "result" : [ { "_id" : null, "number" : 1072 } ], "ok" : 1 }

Get the the most liked user

Say that each like represents the id of another user. How do you find out which user has been liked the most times by other users? It’s easy to see which user has done the most liking (that’s the user with the most “likes” in his/her array), but how would you check to see which user shows up most frequently in other user’s likes?

MongoDB’s aggregation framework to the rescue!

This will give us the top 10 most liked users on our system.
What are we doing here?

  • $unwind the array
  • Create a new $group where the _id is the like’s id
  • $sum the number of times that each id appears throughout your DB
  • $sort by that number, ordered by greatest to least
  • $limit it to the top 10 results.

Your result will look like this (except with 10 results in the array instead of 3)

Let me know if you have any questions in the comments