0
0
MongoDBquery~10 mins

Pipeline execution order matters in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pipeline execution order matters
Start with collection
Apply first pipeline stage
Transform documents
Apply second pipeline stage
Transform documents
Final output after last stage
Documents flow through each pipeline stage in order; changing the order changes the final result.
Execution Sample
MongoDB
db.sales.aggregate([
  { $match: { status: "A" } },
  { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])
Filters sales with status 'A' first, then groups by customer to sum amounts.
Execution Table
StepPipeline StageInput DocumentsActionOutput Documents
1{ $match: { status: "A" } }[{cust_id:1, status:"A", amount:100}, {cust_id:2, status:"B", amount:200}, {cust_id:1, status:"A", amount:50}]Filter documents where status is 'A'[{cust_id:1, status:"A", amount:100}, {cust_id:1, status:"A", amount:50}]
2{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }[{cust_id:1, status:"A", amount:100}, {cust_id:1, status:"A", amount:50}]Group by cust_id and sum amounts[{_id:1, total:150}]
3End of pipeline[{_id:1, total:150}]Return final documents[{_id:1, total:150}]
💡 Pipeline ends after last stage; documents transformed step-by-step.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Documents[{cust_id:1, status:"A", amount:100}, {cust_id:2, status:"B", amount:200}, {cust_id:1, status:"A", amount:50}][{cust_id:1, status:"A", amount:100}, {cust_id:1, status:"A", amount:50}][{_id:1, total:150}][{_id:1, total:150}]
Key Moments - 2 Insights
Why does changing the order of $match and $group change the result?
Because $match filters documents first, reducing data for $group. If $group runs first, it groups all documents, including unwanted ones, changing totals. See execution_table rows 1 and 2.
What happens if $group is placed before $match in this example?
Grouping happens on all documents, including those with status 'B', so totals include unwanted data. This differs from filtering first as shown in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after the first pipeline stage?
A[{_id:1, total:150}]
B[{cust_id:2, status:"B", amount:200}]
C[{cust_id:1, status:"A", amount:100}, {cust_id:1, status:"A", amount:50}]
D[]
💡 Hint
Check the Output Documents column in row 1 of execution_table.
At which step does the documents get grouped by cust_id?
AStep 2
BStep 1
CStep 3
DNo grouping occurs
💡 Hint
Look at the Pipeline Stage column and Action in execution_table row 2.
If we swap $group and $match stages, what changes in the output?
AOutput remains the same
BGrouping includes documents with status 'B'
CNo documents are returned
DPipeline fails with error
💡 Hint
Refer to key_moments explanation about stage order impact.
Concept Snapshot
MongoDB aggregation pipelines process documents stage by stage.
Order matters: earlier stages filter or transform data for later stages.
$match filters documents; $group aggregates them.
Changing order changes results.
Always filter early to optimize and get correct output.
Full Transcript
In MongoDB aggregation pipelines, documents flow through each stage in the order written. Each stage transforms the documents before passing them on. For example, applying $match first filters documents, so the next $group stage only processes filtered data. If you reverse the order, $group aggregates all documents, including unwanted ones, changing the final result. This visual trace shows how documents change after each stage, highlighting why pipeline order matters.