How does an aggregation pipeline work?
Understand the concept
An aggregation pipeline transforms documents through an ordered series of stages. Each stage filters, reshapes, groups or combines the previous output, allowing reporting work to run close to the data. Stage order matters for both the meaning of the result and the amount of work required.
Important ideas
-
Each stage takes the previous stage's output.
-
$matchfilters,$groupcalculates totals, and$sortorders results. -
Filter early when possible;
$limitcaps output and$projectselects fields. -
Moving a filter before a group is only valid when it filters original rows rather than values computed by the group.
Can you explain it without the notes?
How does an aggregation pipeline work?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
An aggregation pipeline transforms documents through an ordered series of stages. Each stage filters, reshapes, groups or combines the previous output, allowing reporting work to run close to the data. Stage order matters for both the meaning of the result and the amount of work required.
Turn this concept into a clear answer.
- Walk through the shape of one document after each important stage before discussing optimization.
Adapted from 04_MONGODB_MONGOOSE.md · “How does an aggregation pipeline work?”. Examples are study fragments; framework behavior can depend on your version.