$avg accumulator in MongoDB - Time & Space Complexity
When using the $avg accumulator in MongoDB, it's important to understand how the time to calculate the average changes as the amount of data grows.
We want to know how the work done by $avg scales when more documents are processed.
Analyze the time complexity of the following MongoDB aggregation snippet.
db.sales.aggregate([
{ $group: { _id: "$store", averageSales: { $avg: "$amount" } } }
])
This groups sales by store and calculates the average sales amount for each store.
Look at what repeats as the data grows.
- Primary operation: Summing the
amountvalues for each document in a group. - How many times: Once for each document in the input collection.
As the number of documents increases, the amount of work to sum and count grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sums and counts |
| 100 | 100 sums and counts |
| 1000 | 1000 sums and counts |
Pattern observation: The work grows in a straight line with the number of documents.
Time Complexity: O(n)
This means the time to calculate the average grows directly with the number of documents processed.
[X] Wrong: "Calculating an average is instant no matter how many documents there are."
[OK] Correct: Even though the average is just one number, MongoDB must look at every document's value to add them up and count them first.
Understanding how aggregation stages like $avg scale helps you explain performance in real projects and shows you think about data size impact clearly.
What if we added a $match stage before $group to filter documents? How would that affect the time complexity?