← Lessons

quiz vs the machine

Gold1380

Databases

The Aggregation Pipeline

Transforming documents through a sequence of staged operations.

6 min read · core · beat Gold to climb

A Pipeline of Stages

The aggregation pipeline processes documents through an ordered list of stages. Each stage takes the stream of documents from the previous stage, transforms it, and passes the result on, much like a Unix pipe.

Common Stages

  • match filters documents, ideally early so it can use an index.
  • project reshapes documents by adding, removing, or computing fields.
  • group buckets documents by a key and computes aggregates like sum and average.
  • sort, limit, and skip order and page results.
  • lookup joins documents from another collection.

Why Order Matters

Placing match and limit early shrinks the document stream before expensive stages run. The optimizer can reorder some stages, but you should still write pipelines so selective filters come first.

Aggregation can do far more than simple finds, including reshaping nested data, computing running totals, and faceted analytics, all on the server close to the data.

Key idea

The aggregation pipeline streams documents through ordered stages like match, group, and lookup, and filtering early keeps later stages fast.

Check yourself

Answer to earn rating on the learn ladder.

1. How does the aggregation pipeline process documents?

2. Why place a match stage early in a pipeline?