When to use MongoDB aggregations instead of queries
Tags: mongodb
Published: 2022-01-26 18:45:54

---
title: When to use MongoDB aggregations instead of queries
published: true
date: 2022-01-24 01:54:50 UTC
tags: MongoDB
canonical_url: https://donaldfeury.xyz/when-to-use-mongodb-aggregations-instead-of-queries/
series: Small Bytes of MongoDB
---

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](https://donaldfeury.xyz/how-to-bulk-insert-documents-into-a-mongodb-collection/).**

```javascript
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.

>💡 **You can read more about [using the find method here](https://donaldfeury.xyz/how-to-retrieve-specific-documents-from-mongodb-using-filters/).**

```javascript
db.podcasts.find({category: "Business"});
```

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

```javascript
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:

```javascript
db.podcasts.aggregate([
	{ $lookup: {
		from: "episodes",
		localField: "id",
		foreignField: "podcast_id",
		as: "episodes"
	}}
]);

```