0
0
MongoDBquery~10 mins

$push accumulator for building arrays in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $push accumulator for building arrays
Start aggregation
Group documents by key
For each group
$push adds each value to array
Build array of values
Output grouped document with array
The $push accumulator collects values from grouped documents into an array for each group.
Execution Sample
MongoDB
db.sales.aggregate([
  { $group: {
      _id: "$item",
      allPrices: { $push: "$price" }
  }}
])
Groups sales by item and collects all prices into an array for each item.
Execution Table
StepInput DocumentGroup Key (_id)Value to PushArray StateAction
1{"item": "apple", "price": 10}apple10[10]Start new group 'apple', push 10
2{"item": "banana", "price": 5}banana5[5]Start new group 'banana', push 5
3{"item": "apple", "price": 15}apple15[10, 15]Push 15 to 'apple' array
4{"item": "banana", "price": 7}banana7[5, 7]Push 7 to 'banana' array
5{"item": "apple", "price": 8}apple8[10, 15, 8]Push 8 to 'apple' array
6No more documents---Aggregation ends, output groups with arrays
💡 All documents processed, arrays built for each group.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
apple array[][10][10][10, 15][10, 15][10, 15, 8][10, 15, 8]
banana array[][][5][5][5, 7][5, 7][5, 7]
Key Moments - 3 Insights
Why does $push create an array even if there is only one document in the group?
$push always collects values into an array for consistency, so even one value is inside an array as shown in step 1 and 2 of the execution_table.
What happens if the group key (_id) changes between documents?
A new group is started for each unique _id, so separate arrays are built independently as seen for 'apple' and 'banana' groups in the execution_table.
Can $push add duplicate values to the array?
Yes, $push adds every value it sees without removing duplicates, so arrays may contain repeated values if input documents have them.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'apple array' after step 3?
A[15]
B[10]
C[10, 15]
D[10, 15, 8]
💡 Hint
Check the 'Array State' column for step 3 in the execution_table.
At which step does the 'banana array' first get a value pushed?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Group Key (_id)' and 'Array State' columns for 'banana' in the execution_table.
If a new document {"item": "apple", "price": 20} is added after step 5, what will be the final 'apple array'?
A[10, 15, 8, 20]
B[10, 15, 8]
C[20]
D[10, 15]
💡 Hint
Consider how $push adds each new value to the existing array as shown in the execution_table.
Concept Snapshot
$push accumulator in MongoDB aggregation
- Used inside $group stage
- Collects values from documents into an array
- Creates array even if one value
- Arrays can have duplicates
- Useful to gather all values per group
Full Transcript
The $push accumulator in MongoDB aggregation collects values from grouped documents into arrays. When grouping documents by a key, $push adds each value to an array for that group. For example, grouping sales by item and pushing prices builds arrays of prices per item. Each input document is processed: if the group key is new, a new array starts; if existing, the value is appended. This continues until all documents are processed, resulting in grouped documents with arrays of values. $push always creates arrays, even if only one value exists, and it includes duplicates if present. This behavior is shown step-by-step in the execution table and variable tracker, helping beginners understand how arrays build up during aggregation.