Learn › Aggregation
The aggregation pipeline
Stages, order, and reshaping documents with $match and $project.
By the end: Read a pipeline top to bottom and predict the shape at every stage.
Lessons
- find() vs aggregate()
find() vs aggregate() in MongoDB: what find can and cannot do, and the point at which you need a pipeline.
- What a pipeline actually is
A MongoDB aggregation pipeline is an ordered list of stages, each transforming the documents the previous stage produced.
- $match, and why it goes first
MongoDB $match filters documents inside an aggregation pipeline, using the same operators as find(). Put it first.
- $project and computed fields
MongoDB $project reshapes documents: include and exclude fields, rename them, and build new computed fields.
- $set and $addFields
MongoDB $set and $addFields add computed fields while keeping everything else. The same stage under two names.
- $sort, $limit, $skip — order matters
MongoDB $sort, $limit and $skip as pipeline stages, and why swapping their order changes the answer, not just the speed.