What does a stage mean in aggregation?
A stage in MongoDB's aggregation is a single step in the data-processing pipeline that performs a specific action on the input documents and then passes the result to the next stage.
How to think about it
- there's a stream of documents
- a stage does something to that stream (filters, sorts, groups, and so on)
- the result immediately moves on down the pipeline
Examples of stages
| Stage | What it does |
|---|---|
$match | filters data |
$group | grouping and aggregate calculations |
$sort | sorting |
$project | selecting and transforming fields |
$limit | limiting the number of documents |
A mini example
js
[
{ $match: { status: "active" } },
{ $sort: { created_at: -1 } }
]$matchkeeps only the documents you need$sortsorts the ones that are left
The main idea
A stage is a single operation inside the aggregation pipeline, and every stage has a strictly defined job to do with the data stream.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.