What does the $project stage do?
The $project stage builds a new "projection" of the document, selecting, renaming, or computing the fields that continue down the pipeline. Conceptually, this is the analog of SELECT in SQL.
What it can do
- keep only the fields you need
- hide fields you don't need
- create computed fields
- rename fields
- nest or flatten data differently
Example
js
{
$project: {
name: 1,
totalPrice: { $multiply: ["$price", "$qty"] }
}
}After $project, the document keeps only name and the new totalPrice field.
The key idea
$project is the stage that shapes the final set of fields, letting you simplify a document, reassemble it, and prepare it for the next stage or the final response.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.