0
0
MongoDBquery~10 mins

$avg accumulator in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $avg accumulator
Start with documents
Group documents by key
Extract values of field to average
Sum values and count documents
Calculate average = sum / count
Output average per group
The $avg accumulator groups documents, sums values, counts them, then divides sum by count to get average.
Execution Sample
MongoDB
db.sales.aggregate([
  { $group: { _id: "$item", avgQuantity: { $avg: "$quantity" } } }
])
Calculate average quantity sold per item from sales documents.
Execution Table
StepDocumentGroup _idQuantitySumCountAverage
1{item: 'apple', quantity: 5}apple5515
2{item: 'banana', quantity: 3}banana3313
3{item: 'apple', quantity: 7}apple71226
4{item: 'banana', quantity: 2}banana2522.5
5{item: 'apple', quantity: 4}apple41635.33
6No more documents-----
💡 All documents processed; averages computed as sum/count per group.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
sum_apple05512121616
count_apple0112233
avg_apple-55665.335.33
sum_banana0033555
count_banana0011222
avg_banana--332.52.52.5
Key Moments - 2 Insights
Why does the average for 'apple' change after each document?
Because each new 'apple' document updates sum and count, changing average as sum/count (see execution_table rows 1,3,5).
What happens if a group has only one document?
The average equals that document's value since sum and count are both 1 (see banana at step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the average quantity for 'banana'?
A3
B2.5
C5
D1
💡 Hint
Check the 'Average' column for 'banana' at step 4 in execution_table.
At which step does the average for 'apple' first become less than 6?
AStep 5
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Average' values for 'apple' in execution_table rows 3 and 5.
If a new document {item: 'banana', quantity: 5} is added after step 5, what happens to banana's average?
AIt stays the same
BIt decreases
CIt increases
DIt becomes zero
💡 Hint
Adding quantity 5 increases sum and count for banana, so average = new sum / new count.
Concept Snapshot
$avg accumulator in MongoDB groups documents by a key.
It sums the values of a field and counts documents.
Then it divides sum by count to get average.
Syntax: { $avg: "$fieldName" } inside $group stage.
Useful to find average values per group in collections.
Full Transcript
The $avg accumulator in MongoDB is used inside an aggregation pipeline's $group stage. It groups documents by a specified key, then calculates the average of a numeric field within each group. This happens by summing the field values and counting the documents, then dividing sum by count. For example, grouping sales by item and averaging quantity sold per item. The execution table shows step-by-step how each document updates sums, counts, and averages. Beginners often wonder why averages change after each document; it's because sums and counts update incrementally. Also, if a group has only one document, average equals that document's value. Adding more documents changes sums and counts, thus changing averages accordingly.