PRO 30% Free • PayPal ($15) & Razorpay (₹899)
30%

MongoDB Aggregation Pipelines: A Hands-on Tutorial

Top Course Staff · 4 min read · Web development · PRO · 30% Free Preview Available

MongoDB Aggregation Pipelines: A Hands-on Tutorial

Analyzing data and extracting metrics requires several tasks such as filtering, sorting, grouping, etc. In MongoDB, we can perform these operations using aggregation pipelines.

Before we dive into the details, to understand different stages in MongoDB aggregation pipelines and to prepare the dataset, read this article onstages in MongoDB Aggregation pipelines.

Let’s discuss how to break complex analytical problems into smaller, manageable parts and how to utilize the different stages in MongoDB aggregation pipelines to gain insights from data. We’ll explore three analytical questions to illustrate how to work with MongoDB aggregation pipelines effectively.

What Are Aggregation Pipelines in MongoDB?

MongoDB aggregation pipelines consist of one or morestagesthat perform specific operations on documents. A stage in the MongoDB pipeline can filter data, group documents, sort them, or select specific fields from the documents.

  • We use the$matchstage to filter the documents.
  • We use the$groupstage to group the data by values in specific fields.
  • We use the$sortstage to sort the documents in the data by a particular field.
  • We use the$projectstage to select specific fields from the documents.

We use theaggregate()function to combine all the stages together and execute the MongoDB aggregation pipeline.

Using Different Stages to Create Aggregation Pipelines in MongoDB

To solve complex problems, we create MongoDB aggregation pipelines with different stages like$match,$group,$sort, and others. We also use operators, such as$sum,$min,$max, and$avg.

To efficiently use aggregation pipelines to solve a problem, we follow this three-step process:

  • Break the problem into small sub-problems like filtering, sorting, and selecting.
  • Identify which MongoDB aggregation pipeline stage we can use to solve each sub-problem.
  • Write the code to solve each sub-problem and combine them to build the pipeline to solve the original problem.

To understand the above steps, let’s consider the following examples:

  • Find the average salary of employees in the department ID 100 for the given employee data.
  • Find the average salary of the employees in each department of an organization and show the values in descending order of the average salary.
  • Show the name and salary of all the employees in department 100. Order the results by employee age in ascending order.

Using the$match,$group, and$projectStages Together

We will use the following steps to create a MongoDB aggregation pipeline for finding the average salary of employees in a department withid100:

  • First, we will use the$matchstage to filter documents for departmentid100.
  • Next, we will use the$groupstage to group all the documents with departmentid100. For this, we will passnullto the_idkey in the$groupstage. Subsequently, we will use the$avgoperator to calculate the average salary for the given department.
  • Finally, we will use the$projectstage to show the average salary value.

An example of this is as follows:

codecademy> db.employees.aggregate([{$match:{"dept_id":100}},  {$group:{_id:null, average_salary:{$avg:"$salary"}}},  {$project:{_id:0,average_salary:1}}])  
Copy to clipboard
codecademy> db.employees.aggregate([{$match:{"dept_id":100}},  {$group:{_id:null, average_salary:{$avg:"$salary"}}},  {$project:{_id:0,average_salary:1}}])

Output:

[ { average_salary: 11750 } ]  
Copy to clipboard
[ { average_salary: 11750 } ]

Using the$group,$sort, and$projectStages Together

To calculate the average salary in each department of the organization and show the values in descending order of the average salary, we will build a MongoDB aggregation pipeline using the$group,$sort, and$projectstages. For this, we use the following steps:

  • First, we will use the$groupstage and the$avgoperator to find the average salary in each department.
  • Next, we will use the$sortstage to sort the data by the average salary field.
  • Finally, we use the$projectstage to show the output.

You can observe this in the following example:

codecademy> db.employees.aggregate([  {$group: {_id: {dept_id:"$dept_id"}, average_salary:{$avg:"$salary"}}},  {$sort:{average_salary:-1}},  {$project:{_id:1, average_salary:1}}])  
Copy to clipboard
codecademy> db.employees.aggregate([  {$group: {_id: {dept_id:"$dept_id"}, average_salary:{$avg:"$salary"}}},  {$sort:{average_salary:-1}},  {$project:{_id:1, average_salary:1}}])
[    { _id: { dept_id: 300 }, average_salary: 22500 },    { _id: { dept_id: 200 }, average_salary: 15000 },    { _id: { dept_id: 400 }, average_salary: 15000 },    { _id: { dept_id: 100 }, average_salary: 11750 }  ]  
Copy to clipboard
[    { _id: { dept_id: 300 }, average_salary: 22500 },    { _id: { dept_id: 200 }, average_salary: 15000 },    { _id: { dept_id: 400 }, average_salary: 15000 },    { _id: { dept_id: 100 }, average_salary: 11750 }  ]

Using the$match,$sort, and$projectStages Together

We will use the$match,$sort, and$projectstages to build a MongoDB aggregation pipeline showing thenameandsalaryof all the employees in department 100, ordered by employee age. We do this like so::

  • We use the$matchstage to filter documents for the department ID 100.
  • We use the$sortstage to sort the documents using theemp_agefield.
  • We will use the$projectstage to show only theemp_nameandsalaryfields.

You can see this in the following example:

codecademy> db.employees.aggregate([{$match:{"dept_id":100}},   {$sort:{emp_age:1}},  {$project:{_id:0,emp_name:1,salary:1}}])  
Copy to clipboard
codecademy> db.employees.aggregate([{$match:{"dept_id":100}},   {$sort:{emp_age:1}},  {$project:{_id:0,emp_name:1,salary:1}}])
[    { emp_name: 'Katy', salary: 12000 },    { emp_name: 'Aditya', salary: 12000 },    { emp_name: 'Adam', salary: 13000 },    { emp_name: 'Ankit', salary: 10000 }  ]  
Copy to clipboard
[    { emp_name: 'Katy', salary: 12000 },    { emp_name: 'Aditya', salary: 12000 },    { emp_name: 'Adam', salary: 13000 },    { emp_name: 'Ankit', salary: 10000 }  ]

Conclusion

Mastering MongoDB aggregation pipelines can help you solve analytical questions easily. Using the examples, we discussed how to break complex analytical problems into sub-problems and solve them using different stages in MongoDB aggregation pipelines.

To better understand the concepts, we suggest you frame some questions to calculate different metrics on any given dataset and create aggregation pipelines for it. This will help you understand how to break problems down, solve them using different stages, and create a complete aggregation pipeline in MongoDB.

To read more tutorials on topics like data science, cloud computing, artificial intelligence, etc., visit theCodecademy article hub.

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

30% Free Preview Reached · Unlock Remaining 70%

Complete Payment to Unlock Full Guide & Source Code

You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.

✓ Full Access to All 160+ Projects
✓ Instant Blur Removal Everywhere
✓ Production Source Code & Blueprints
✓ All Future Deep Dives Included
Instant Unhindered Activation ·
End of Preview · Unlock Complete Blueprint & Source Code

Ready to access the complete step-by-step tutorial?

Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.

Step-by-Step Project Library

Ready to start building your next project?

Enjoyed this guide? Continue exploring our library of 412+ step-by-step guides, starter files, and practical projects across Web Development, Mobile Apps, AI, and Design.