Course contentsShow
← System Design
Lesson 399 of 1,91911. NoSQL: Document StoresFree lesson

Aggregation Pipeline

Using MongoDB's multi-stage aggregation framework to transform and analyze documents with operators like $match and $group.

Aggregation Pipeline

What you'll learn: How MongoDB's aggregation pipeline lets you filter, transform, and summarize documents across multiple processing stages.

The Core Idea

Think of MongoDB's aggregation pipeline like an assembly line in a factory. Your documents enter at one end, pass through multiple stations (stages), and emerge transformed at the other end. Each stage performs one specific operation—filtering, grouping, sorting, or calculating—and passes its output to the next stage.

Unlike simple queries that just retrieve documents, the aggregation pipeline lets you process data within the database itself. This is powerful for analytics, reporting, and complex data transformations.

How It Works

The pipeline consists of stages, each represented by an operator starting with $. Documents flow through these stages sequentially:

$match — Filters documents, like a WHERE clause. Only matching documents proceed to the next stage.

$group — Groups documents by a field and performs calculations (count, sum, average) on each group.

Other stages include $sort, $project (reshape documents), $limit, and $unwind (flatten arrays).

Example Flow

Imagine you have orders with {customer, amount, status}. To find total sales per customer for completed orders:

  1. Stage 1 ($match): Keep only status: "completed" orders
  2. Stage 2 ($group): Group by customer, sum the amount fields

Each stage's output becomes the input for the next, creating a data transformation pipeline.

Why It Matters

Instead of retrieving all documents and processing them in your application code (slow, inefficient), the aggregation pipeline does heavy lifting inside MongoDB, close to the data. It's optimized and can leverage indexes at early stages.

Key Takeaway: MongoDB's aggregation pipeline processes documents through multiple ordered stages like $match and $group, enabling complex analytics and transformations directly within the database.