Aggregation for reporting dashboards in MongoDB - Time & Space Complexity
When building reporting dashboards, we often use aggregation to summarize data quickly.
We want to know how the time to get these summaries grows as the data grows.
Analyze the time complexity of the following aggregation pipeline.
db.sales.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: "$region", totalSales: { $sum: "$amount" } } },
{ $sort: { totalSales: -1 } }
])
This pipeline filters completed sales, groups them by region summing amounts, then sorts by total sales.
Look for repeated work inside the pipeline.
- Primary operation: Scanning each sale document once to filter and group.
- How many times: Once per document in the sales collection.
As the number of sales grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks and group updates |
| 100 | About 100 document checks and group updates |
| 1000 | About 1000 document checks and group updates |
Pattern observation: The work grows roughly in direct proportion to the number of sales.
Time Complexity: O(n)
This means the time to run the aggregation grows linearly with the number of sales records.
[X] Wrong: "Grouping by region makes the query slower by the square of the data size."
[OK] Correct: Grouping just collects data as it scans once; it does not multiply work by data size again.
Understanding how aggregation scales helps you explain how dashboards stay fast even with lots of data.
"What if we added a $lookup stage to join with another collection? How would the time complexity change?"