DEV Community

Donald Feury
Donald Feury

Posted on • Originally published at donaldfeury.xyz on

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 use with the following examples you can insert the following documents into your MongoDB instance:

💡 If you haven't inserted multiple documents into MongoDB before, you can read about that here.

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"}
]);
Enter fullscreen mode Exit fullscreen mode

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.

💡 You can read more about using the find method here.

db.podcasts.find({category: "Business"});
Enter fullscreen mode Exit fullscreen mode

To do the same thing with an aggregation would require the use of $match aggregation stage.

db.podcasts.aggregate([
 { $match: {
    category: "Business"
 }}
]);
Enter fullscreen mode Exit fullscreen mode

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"
    }}
]);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)