0
0
MongoDBquery~10 mins

Computed pattern for pre-aggregation in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Computed pattern for pre-aggregation
Start: Raw Data Collection
Define Computed Fields
Apply Aggregation Pipeline
Compute and Store Aggregated Results
Use Pre-Aggregated Data for Queries
End
This flow shows how raw data is processed by defining computed fields, aggregating them, storing results, and then using these pre-aggregated results for faster queries.
Execution Sample
MongoDB
db.sales.aggregate([
  { $project: {
      item: 1,
      totalPrice: { $multiply: ["$price", "$quantity"] }
    }
  },
  { $group: { _id: "$item", totalSales: { $sum: "$totalPrice" } } }
])
This MongoDB aggregation computes total price per sale and then sums total sales per item.
Execution Table
StepPipeline StageInput DocumentComputed FieldGroup KeyAggregated Value
1$project{"item":"apple", "price":2, "quantity":3}totalPrice = 2*3=6
2$project{"item":"banana", "price":1, "quantity":5}totalPrice = 1*5=5
3$project{"item":"apple", "price":2, "quantity":2}totalPrice = 2*2=4
4$group{"item":"apple", "totalPrice":6}apple6
5$group{"item":"banana", "totalPrice":5}banana5
6$group{"item":"apple", "totalPrice":4}apple6+4=10
7EndFinal aggregated totals: apple=10, banana=5
💡 All documents processed; aggregation complete with pre-aggregated totals per item.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
totalPriceN/A654N/AN/AN/AN/A
totalSales_apple0661010101010
totalSales_banana00555555
Key Moments - 2 Insights
Why do we compute 'totalPrice' before grouping?
Computing 'totalPrice' in the $project stage creates a new field for each document, which can then be summed in the $group stage. This separation ensures accurate aggregation as shown in execution_table rows 1-3 for computation and 4-6 for grouping.
How does the $group stage accumulate totals?
The $group stage sums 'totalPrice' values for each unique 'item' key. For example, in rows 4 and 6, 'apple' totals are added step-by-step to reach the final sum 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the computed 'totalPrice' for the second document at step 2?
A6
B4
C5
D10
💡 Hint
Check the 'Computed Field' column at step 2 in the execution_table.
At which step does the total sales for 'apple' become 10?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the 'Aggregated Value' for 'apple' in the execution_table rows.
If the quantity of the first 'apple' document changed from 3 to 4, what would be the new 'totalPrice' at step 1?
A8
B10
C6
D12
💡 Hint
Multiply price 2 by new quantity 4 as shown in the 'Computed Field' logic.
Concept Snapshot
MongoDB pre-aggregation uses $project to compute new fields (e.g., totalPrice = price * quantity).
Then $group sums these computed fields by keys (e.g., item).
This pattern speeds up queries by storing aggregated results.
Use $project before $group to prepare data for aggregation.
Final output is grouped totals ready for fast retrieval.
Full Transcript
This visual execution traces the MongoDB computed pattern for pre-aggregation. Starting with raw sales data, each document computes a totalPrice by multiplying price and quantity in the $project stage. Then, the $group stage sums totalPrice values grouped by item names. The execution table shows each step: computing totalPrice for each document, then accumulating sums per item. Variable tracking reveals how totalPrice and totalSales for each item change after each step. Key moments clarify why computation happens before grouping and how sums accumulate. The visual quiz tests understanding of computed values and aggregation steps. This pattern helps store pre-aggregated data for faster queries.