MongoDB Aggregation Guide: A Friendly Explanation

MongoDB aggregation is a powerful tool for processing and transforming data within a collection. It allows you to perform complex queries, grouping, filtering, sorting, and calculations in a streamlined way.

This guide will cover all major MongoDB aggregation scenarios in simple terms with examples.


What is Aggregation?

Aggregation in MongoDB is like an advanced search feature that helps retrieve and manipulate data efficiently. Instead of fetching raw data, you can apply operations to shape the output according to your needs.

MongoDB uses the Aggregation Pipeline, which processes data through a series of stages. Each stage modifies or filters the data before passing it to the next stage.

Basic Syntax:

db.collection.aggregate([ { stage1 }, { stage2 }, { stage3 } ])

Common Aggregation Stages & Scenarios

1️⃣ $match – Filtering Data

Use $match to filter documents based on a condition, similar to find().

Example: Find students who are older than 21.

db.Students.aggregate([
  { $match: { age: { $gt: 21 } } }
])

2️⃣ $group – Grouping Data

The $group stage groups documents by a specific field and applies operations like counting, summing, or averaging.

Example: Count the number of students in each class.

db.Students.aggregate([
  { $group: { _id: "$class", totalStudents: { $sum: 1 } } }
])

Example: Find the average age of students.

db.Students.aggregate([
  { $group: { _id: null, avgAge: { $avg: "$age" } } }
])

3️⃣ $sort – Sorting Data

Sorts documents based on a field in ascending (1) or descending (-1) order.

Example: Sort students by age in descending order.

db.Students.aggregate([
  { $sort: { age: -1 } }
])

4️⃣ $project – Reshaping Data

Used to include or exclude specific fields from the output.

Example: Show only names and ages of students, excluding _id.

db.Students.aggregate([
  { $project: { name: 1, age: 1, _id: 0 } }
])

5️⃣ $limit – Limiting Results

Limits the number of documents in the output.

Example: Show only the first 5 students.

db.Students.aggregate([
  { $limit: 5 }
])

6️⃣ $skip – Skipping Documents

Skips a specified number of documents before returning results.

Example: Skip the first 3 students and return the rest.

db.Students.aggregate([
  { $skip: 3 }
])

7️⃣ $unwind – Splitting Array Fields

Expands an array field, creating separate documents for each item in the array.

Example: If a student document contains multiple enrolled classes:

{
  "name": "Alice",
  "classes": ["Math 101", "Physics 201"]
}

Using $unwind, each class is separated into a new document:

db.Students.aggregate([
  { $unwind: "$classes" }
])

8️⃣ $lookup – Joining Collections

Performs a left join to combine data from another collection.

Example: Get class details for each student by referencing the Classes collection.

db.Students.aggregate([
  {
    $lookup: {
      from: "Classes",
      localField: "class",
      foreignField: "name",
      as: "classDetails"
    }
  }
])

9️⃣ $facet – Multi-Pipeline Processing

Runs multiple aggregation pipelines and returns results in a single document.

Example: Get the total number of students and the average age in one query.

db.Students.aggregate([
  {
    $facet: {
      totalStudents: [{ $count: "count" }],
      averageAge: [{ $group: { _id: null, avgAge: { $avg: "$age" } } }]
    }
  }
])

🔟 $out – Saving Results

Saves the aggregation results into a new collection.

Example: Store the total number of students per class into a new collection ClassSummary.

db.Students.aggregate([
  { $group: { _id: "$class", totalStudents: { $sum: 1 } } },
  { $out: "ClassSummary" }
])

Putting It All Together: A Complex Example

Let's combine multiple stages to get meaningful insights.

Scenario: Get the top 3 oldest students from each class.

db.Students.aggregate([
  { $sort: { age: -1 } },
  { $group: { _id: "$class", topStudents: { $push: "$name" } } },
  { $project: { class: "$_id", topStudents: { $slice: ["$topStudents", 3] }, _id: 0 } }
])

Conclusion

MongoDB aggregation is a powerful way to analyze and transform data. Using different pipeline stages like $match, $group, $lookup, and $sort, you can perform complex queries efficiently.

Understanding these concepts will help you work smarter with MongoDB! 🚀