0
0
MongoDBquery~10 mins

$project stage for shaping output in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $project stage for shaping output
Input Documents
$project Stage
Select Fields
Rename or Create Fields
Output Documents with New Shape
The $project stage takes input documents, selects or reshapes fields, and outputs documents with the new structure.
Execution Sample
MongoDB
db.orders.aggregate([
  { $project: { item: 1, total: { $multiply: ["$price", "$quantity"] } } }
])
This pipeline reshapes each order document to keep 'item' and add a new 'total' field calculated by multiplying price and quantity.
Execution Table
StepInput DocumentActionProjected FieldsOutput Document
1{"item": "apple", "price": 2, "quantity": 5, "category": "fruit"}Keep 'item', compute 'total' = price * quantity{"item": "apple", "total": 10}{"item": "apple", "total": 10}
2{"item": "banana", "price": 1, "quantity": 10, "category": "fruit"}Keep 'item', compute 'total' = price * quantity{"item": "banana", "total": 10}{"item": "banana", "total": 10}
3{"item": "carrot", "price": 3, "quantity": 4, "category": "vegetable"}Keep 'item', compute 'total' = price * quantity{"item": "carrot", "total": 12}{"item": "carrot", "total": 12}
4No more documentsEnd of pipelineN/APipeline ends
💡 All input documents processed, pipeline ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Input DocumentN/A{"item": "apple", "price": 2, "quantity": 5, "category": "fruit"}{"item": "banana", "price": 1, "quantity": 10, "category": "fruit"}{"item": "carrot", "price": 3, "quantity": 4, "category": "vegetable"}No more documents
Projected FieldsN/A{"item": "apple", "total": 10}{"item": "banana", "total": 10}{"item": "carrot", "total": 12}N/A
Output DocumentN/A{"item": "apple", "total": 10}{"item": "banana", "total": 10}{"item": "carrot", "total": 12}Pipeline ends
Key Moments - 2 Insights
Why does the output document only have 'item' and 'total' fields, not 'price' or 'quantity'?
Because the $project stage explicitly includes only 'item' and the computed 'total' field, all other fields are excluded by default as shown in execution_table rows 1-3.
How is the 'total' field calculated in each output document?
The 'total' field is computed by multiplying the 'price' and 'quantity' fields from the input document, as seen in the Action column of execution_table rows 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2. What is the value of the 'total' field in the output document?
A10
B1
C11
D0
💡 Hint
Check the 'Projected Fields' column at Step 2 in the execution_table.
At which step does the pipeline finish processing all documents?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look for the row where 'No more documents' is in the Input Document column.
If we added 'category: 1' to the $project stage, what would change in the output documents?
AThe output documents would include the 'category' field as well.
BThe 'total' field would be removed.
CThe input documents would change.
DNothing would change.
💡 Hint
Refer to how $project controls which fields appear in output documents in the execution_table.
Concept Snapshot
$project stage reshapes documents in aggregation.
Specify fields to keep with 1, exclude others.
Create new fields with expressions.
Output documents have only projected fields.
Useful for selecting, renaming, or computing fields.
Full Transcript
The $project stage in MongoDB aggregation takes each input document and reshapes it by selecting specific fields to keep or exclude. It can also create new fields using expressions like multiplication. In the example, the pipeline keeps the 'item' field and adds a new 'total' field calculated by multiplying 'price' and 'quantity'. Each input document is processed step-by-step, producing output documents with only the projected fields. The pipeline ends after all documents are processed.