MongoDB Aggregation In-Depth: Fundamentals and Pipeline Mastery
Tags: webdev, mongodb, database, computerscience
Published: 2023-11-04 07:50:12



![cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wujiilyerilxb9zjdc1e.png)
Analysing data and transforming raw data to useful information is one of the key task for a developer. In the case of sql we generally write queries to filter data based upon our requirements but what in the case of MongoDB? Here comes the part of aggregation. It is a framework for analysing the data and getting meaningful results. Aggregation can be divided in two segments
* _Aggregation Operators_ - These are specialized tools in MongoDB that help us reshape, filter, and analyze our data in meaningful ways. 
* _Aggregation Pipeline_ - In MongoDB, think of the Aggregation Pipeline as a conveyor belt for our data. It's a step-by-step process where each operator is like a worker performing a specific job on our data. These jobs could be sorting, grouping, or other tasks. The pipeline helps transform your raw data into organized and insightful outcomes.

### Prerequisites

1. **Basics of MongoDB** - Before diving into aggregation, ensure you're familiar with MongoDB's basic operations. This includes inserting, updating, querying, and deleting documents. If you're new to MongoDB, please refer to the [MongoDB Official Documentation.](https://www.mongodb.com/docs/manual/crud).

2. **MongoDB & MongoDB Compass Installed** - MongoDB server and shell should be running to ensure the working of aggregations. Compass is GUI for MongoDB which provides a way to run queries and visualize the data.

### Aggregation Pipeline

The Aggregation Pipeline is a sequence of stages, with each stage represented as operators. These operators are assigned a specific task. As the data moves through the stages, it undergoes series of transformations and processed data in each stage is passed to the next stage. Some of those operators could be $match, $project, $sort etc.

Now we'll build a database from scratch and discuss each operator based on that.

So, I have created a collection named students and structure of each student is like 
```
{
 "first_name":"Michel",
 "last_name":"Pirouet",
 "email":"mpirouet0@adobe.com",
 "gender":"Female"
}
```
I'll be performing all the operations on this type of dataset and attach the result.

### Aggregation Operators

Before moving to the operators firstly we'll discuss the structure of a pipeline. From the context of javascript it is just an array of objects where each object represents a stage.

```
[
  {
    $match:
      {
        gender: "Female",
      },
  },
]
```
This is an example of pipeline where $match is a operator which ensures that only students with gender as female are passed to next stage. This structure is the foundation of pipeline and it will remain same for all the operators. Now we'll move ahead to discuss each operator in detail.

**$match** :- The '$match' operator acts as a digital filter allowing us to shift the data based upon specific criteria. Taking example of above snippet, gender: Female is one criteria. Moreover we can add several other conditions based on our requirements.

***$project*** :- Once the data is filtered the next requirement is generally structuring the output, Sometimes we need to return specific keys in the output and $project helps us achieve it.

In a scenario where we aim to retrieve only the first name and gender of female students, the initial stage would involve using the $match operator. Following that, the second stage would incorporate the $project operator to refine the output, possibly resembling the following:

```
[
  {
    $match:
      {
        gender: "Female",
      },
  },
  {
    $project: {
       first_name : 1,
       gender : 1
    }
  }
]
```

![Result of $project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ss250gtsnsdq1t5ckcap.png)


***$group*** :- This is one of the most crucial operators in aggregation. Its primary purpose is to group documents based on specific fields, enabling meaningful data analysis. Similar to SQL constraints, it's important to note that when using $group, only the fields mentioned in $project, which have also been used in $group, are allowed.

Furthermore, aggregation incorporates group accumulator operators such as $sum, $max, $min, etc. These operators are designed to execute calculations on grouped documents, adding a layer of precision to the aggregation process.

For the group aggregation example, I will add a 'marks' field to each student in the database. Subsequently, we will calculate results such as the maximum and average marks based upon gender. This involves grouping the documents by gender and utilizing group accumulator operators like $max and $avg on the 'marks' field.

```
[
  {
    $group: {
      _id : '$gender',
      maxMarks: { $max: "$marks" },
      avgMarks: { $avg: "$marks" },
      minMarks : {$min : "$marks"}
    }
  }
]
```

In this particular pipeline _id serves as the key for grouping documents. The $gender specifies that grouping should be done on gender column. '$' in gender is used as a placeholder that helps MongoDB identify the field name with context to pipeline.

![Result of $group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bw3t28cbnb4s9guhyk3n.png)


***$sort*** :- The $sort operator in aggregation pipeline is used to arrange documents in a specified order. It's like organizing a set of documents based on certain criteria, be it ascending or descending values of a particular field. For instance, { $sort: { score: -1 } } would arrange documents by the "score" field in descending order, placing the highest scores first. This operator is essential for structuring data output in a way that facilitates easier analysis and interpretation.

![Result of $sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxh9bsyunwmqdtphvqws.png)



### Wrapping Up: A Sneak Peek into Future Aggregation Topics

we've deep dived into the fundamental operations of MongoDB Aggregation, exploring the power of $match for precise data filtering, $sort for organizing results, $project for shaping the output, and $group for comprehensive data grouping. These operations form the backbone of MongoDB's aggregation capabilities, laying the groundwork for more advanced manipulations. In our upcoming articles, we will dive into the mathematical operations, unlocking the potential for complex calculations, and explore array operations, allowing for dynamic handling of arrays within the aggregation pipeline. Stay tuned for a deeper exploration of MongoDB Aggregation as we venture into these advanced topics.