Commonly, when we work with MongoDB and want to retrieve documents from a collection we make use of the method find or a variation combined with some kind of filtering or processing on the code side.
For most cases, the previous technique is going to be good enough, but there will be some other cases where that won’t be enough. For instance:
- The filtering or processing on the code side is becoming expensive due to the number of documents.
- The query involves several collections.
- The resulting document structure doesn’t fit our needs for pot-query operations.
When we are facing any of those situations, we can take advantage of the aggregation operations.
The topic itself is complex and requires some time of study to be understood and applied appropriately. But in order to start understanding the topic, let’s imagine this scenario.
Aggregation pipeline scenario
Let suppose that we have 3 collections: Users, posts and comments. We need to get a list of posts that includes all the posts that belong to each user, together with the comments that belong to each post. We also want to filter those posts by a specific field and a user’s specific field.
Complex for NoSQL huh? With an aggregation pipeline, it’s much easier than you think. The stages would be something similar to these:
- $match // over posts to filter by the specific field
- $lookup // to populate the users (will produce an array of users)
- $unwind // to convert the users array into a plain object
- $match // over users to filter by the specific user’s field
- $lookup // to populate the comments
As you can see, the process is not difficult once you get familiarized with the stages available in this powerful feature.
If you want to learn more about the aggregation operations, please take a look at the official documentation or ask for a post about a specific topic. We’ll be happy to write it for you.
Regos Dev Studio is a product development company that builds add-ons for Jira and Confluence, combined with the development of custom solutions in a variety of languages.