When to use MongoDB aggregations instead of queries
When it comes to reading data out of your MongoDB instance you have two options, a query using the standard query language or an aggregation pipeline. While you can often use either for many use cases, there are differences in how they are used and what use cases they are a good fit for.
For a full overview of MongoDB and all my posts on it, check out my overview.
When it comes to reading data out of your MongoDB instance you have two options, a query using the standard query language or an aggregation pipeline. While you can often use either for many use cases, there are differences in how they are used and what use cases they are a good fit for.
For use with the following examples you can insert the following documents into your MongoDB instance:
db.podcasts.insertMany([
{id: 1, name: "Podcast 1", category: "Business", rss: "https://mypodcast1.com/podcast/rss"},
{id: 2, name: "Podcast 2", category: "Business", rss: "https://podcast2.net/rss"},
{id: 3, name: "Podcast 3", category: "Health & Wellness", rss: "https://podcast3.xyz/feed/rss"},
{id: 4, name: "Podcast 4", category: "Technology", rss: "https://podcast4.com/rss"}
]);
db.episodes.insertMany([
{podcast_id: 3, title: "Anabolic Recipes", published_on: "2020-01-02"},
{podcast_id: 4, title: "Getting Going with Go", published_on: "2021-02-24"},
{podcast_id: 1, title: "Talk About Money Early", published_on: "2022-01-01"},
{podcast_id: 1, title: "Charge More by Saying No", publshed_on: "2021-12-01"},
{podcast_id: 2, title: "Candidate Interview Strategies", published_on: "2020-05-01"},
{podcast_id: 4, title: "The Magic of Pattern Matching", published_on: "2022-01-01"}
]);
Ease of use
MongoDB's query language and its related methods are meant to be straightforward to use. Compared to aggregations they are easier to read and are less prone to error.
Take the example of wanting to filter out and return only certain documents. Using the query language is as simple as passing in an object describing how to filter the objects to the find
method.
db.podcasts.find({category: "Business"});
To do the same thing with an aggregation would require the use of $match
aggregation stage.
db.podcasts.aggregate([
{ $match: {
category: "Business"
}}
]);
Use Case
While the strength of the standard query language is its simplicity, that also limits what problems it can solve. Aggregations are essentially the opposite, harder to use but much more powerful, allowing them to accomplish certain use cases that are unable to be done by simple queries.
The standard query language is a good solution for:
- Returning/affecting only certain documents that match provided filter
- When reading data back out, only including certain fields
Anything that falls outside the scope of those scenarios is either better done or required to be done by aggregations.
One such example and the earliest one I personally needed to use aggregations was when wanting to perform an operation similar to a join in a SQL query.
This simply cannot be done using the available query methods and language but CAN be accomplished using the $lookup
aggregation stage.
What if we wanted to get all of our podcasts including each episode associated with that podcast:
db.podcasts.aggregate([
{ $lookup: {
from: "episodes",
localField: "id",
foreignField: "podcast_id",
as: "episodes"
}}
]);
Did you find this information useful? If so, consider heading over to my donation page and drop me some support.
Want to ask a question or just chat? Contact me here