0
0
MongoDBquery~5 mins

$avg accumulator in MongoDB - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: $avg accumulator
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

Look at what repeats as the data grows.

  • Primary operation: Summing the amount values for each document in a group.
  • How many times: Once for each document in the input collection.
How Execution Grows With Input

As the number of documents increases, the amount of work to sum and count grows proportionally.

Input Size (n)Approx. Operations
1010 sums and counts
100100 sums and counts
10001000 sums and counts

Pattern observation: The work grows in a straight line with the number of documents.

Final Time Complexity

Time Complexity: O(n)

This means the time to calculate the average grows directly with the number of documents processed.

Common Mistake

[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.

Interview Connect

Understanding how aggregation stages like $avg scale helps you explain performance in real projects and shows you think about data size impact clearly.

Self-Check

What if we added a $match stage before $group to filter documents? How would that affect the time complexity?