Bird
Raised Fist0
MongoDBquery~10 mins

$each modifier with $push in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - $each modifier with $push
Start with document
↓
$push with $each detected
↓
Iterate over each element in array
↓
Add element to target array field
↓
Update document with new array
↓
End with updated document
This flow shows how MongoDB uses $push with $each to add multiple elements to an array field one by one.
Execution Sample
MongoDB
db.collection.updateOne(
  { _id: 1 },
  { $push: { scores: { $each: [85, 90, 95] } } }
)
Adds multiple scores (85, 90, 95) to the 'scores' array in the document with _id 1.
Execution Table
StepActionCurrent 'scores' arrayElement AddedResulting 'scores' array
1Start update[80, 82]N/A[80, 82]
2Push 85[80, 82]85[80, 82, 85]
3Push 90[80, 82, 85]90[80, 82, 85, 90]
4Push 95[80, 82, 85, 90]95[80, 82, 85, 90, 95]
5Update complete[80, 82, 85, 90, 95]N/A[80, 82, 85, 90, 95]
💡 All elements in $each array pushed; update operation finished.
Variable Tracker
VariableStartAfter 1After 2After 3Final
scores[80, 82][80, 82, 85][80, 82, 85, 90][80, 82, 85, 90, 95][80, 82, 85, 90, 95]
Key Moments - 2 Insights
Why does $push need $each to add multiple elements?
Without $each, $push adds only one element. The execution_table shows each element added step-by-step using $each (rows 2-4).
Does $push with $each replace the array or add to it?
It adds elements to the existing array, not replace it. See how 'scores' grows after each step in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'scores' array after step 3?
A[80, 82, 85]
B[80, 82, 85, 90]
C[85, 90]
D[80, 82]
💡 Hint
Check the 'Resulting scores array' column at step 3 in execution_table.
At which step does the last element get added to the array?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Element Added' column in execution_table to find when 95 is added.
If we remove $each and try to push [85, 90, 95] directly, what happens?
AEach element is added separately automatically
BUpdate fails with error
CThe entire array [85, 90, 95] is added as one element
DNothing changes in the array
💡 Hint
Recall that $push without $each adds one element; here that element would be the whole array.
Concept Snapshot
$push with $each lets you add multiple elements to an array field.
Syntax: {$push: {field: {$each: [val1, val2, ...]}}}
Without $each, $push adds only one element.
Each element is appended in order.
Useful for batch updates to arrays.
Full Transcript
This visual execution shows how MongoDB's $push operator combined with $each modifier adds multiple elements to an array field in a document. Starting with an array [80, 82], each element from [85, 90, 95] is added one by one, updating the array step-by-step until it becomes [80, 82, 85, 90, 95]. The $each modifier is necessary to push multiple elements; without it, $push would add the entire array as a single element. The execution table and variable tracker clearly show the array's state after each addition, helping beginners understand the incremental update process.

Practice

(1/5)
1. What does the $each modifier do when used with $push in MongoDB?
easy
A. It sorts the array elements after pushing a new value.
B. It adds multiple values to an array in a single update operation.
C. It replaces the entire array with a new set of values.
D. It removes multiple values from an array.

Solution

  1. Step 1: Understand $push usage

    $push adds elements to an array field in a document.
  2. Step 2: Role of $each modifier

    $each allows adding multiple elements at once inside $push, making the update efficient.
  3. Final Answer:

    It adds multiple values to an array in a single update operation. -> Option B
  4. Quick Check:

    $each adds many items [OK]
Hint: Remember: $each adds many items inside $push [OK]
Common Mistakes:
  • Thinking $each removes items
  • Using $each outside $push
  • Confusing $each with sorting
2. Which of the following is the correct syntax to add multiple values ["a", "b", "c"] to the array field tags using $push with $each?
easy
A. { $push: { tags: { $each: ["a", "b", "c"] } } }
B. { $push: { tags: ["a", "b", "c"] } }
C. { $push: { tags: { $add: ["a", "b", "c"] } } }
D. { $push: { tags: { $each: "a", "b", "c" } } }

Solution

  1. Step 1: Check correct $push syntax

    $push requires a field and a value or modifier object.
  2. Step 2: Validate $each usage

    $each must be an array inside $push to add multiple items.
  3. Final Answer:

    { $push: { tags: { $each: ["a", "b", "c"] } } } -> Option A
  4. Quick Check:

    Correct $each syntax = { $push: { tags: { $each: ["a", "b", "c"] } } } [OK]
Hint: Use $each with an array inside $push [OK]
Common Mistakes:
  • Passing array directly without $each
  • Using invalid operator like $add
  • Incorrect $each format without array brackets
3. Given the document { _id: 1, colors: ["red"] }, what will be the colors array after running this update?
{ $push: { colors: { $each: ["blue", "green"] } } }
medium
A. ["red", ["blue", "green"]]
B. ["blue", "green"]
C. ["red", "blue", "green"]
D. ["red"]

Solution

  1. Step 1: Initial array and update operation

    Original array is ["red"]. The update uses $push with $each to add "blue" and "green".
  2. Step 2: Effect of $each inside $push

    $each adds each element individually, so "blue" and "green" are appended to the array.
  3. Final Answer:

    ["red", "blue", "green"] -> Option C
  4. Quick Check:

    Array after update = ["red", "blue", "green"] [OK]
Hint: $each adds items individually, not as a nested array [OK]
Common Mistakes:
  • Expecting nested array inside colors
  • Replacing array instead of pushing
  • Ignoring original array elements
4. What is wrong with this update operation?
{ $push: { items: { $each: "apple", "banana" } } }
medium
A. The $each value must be an array, not separate strings.
B. The $push operator cannot be used with $each.
C. The field name items is invalid.
D. The update syntax is correct and will work as expected.

Solution

  1. Step 1: Analyze $each value type

    $each requires an array of values, but here it is given as separate strings without array brackets.
  2. Step 2: Identify syntax error

    Passing multiple strings without array brackets causes a syntax error in MongoDB update.
  3. Final Answer:

    The $each value must be an array, not separate strings. -> Option A
  4. Quick Check:

    $each needs array brackets [OK]
Hint: Always wrap $each values in square brackets [OK]
Common Mistakes:
  • Passing multiple values without array brackets
  • Confusing $each with $addToSet
  • Assuming $push alone can add multiple items
5. You want to add multiple new tags ["urgent", "review", "backend"] to the tags array of a document, but only if they are not already present. Which update operation correctly uses $push with $each and ensures no duplicates?
hard
A. { $push: { tags: { $each: ["urgent", "review", "backend"] } } }
B. { $addToSet: { tags: ["urgent", "review", "backend"] } }
C. { $push: { tags: ["urgent", "review", "backend"] } }
D. { $addToSet: { tags: { $each: ["urgent", "review", "backend"] } } }

Solution

  1. Step 1: Understand requirement to avoid duplicates

    $push adds items regardless of duplicates, so it doesn't fit the requirement.
  2. Step 2: Use $addToSet with $each

    $addToSet adds unique values only, and supports $each to add multiple items without duplicates.
  3. Final Answer:

    { $addToSet: { tags: { $each: ["urgent", "review", "backend"] } } } -> Option D
  4. Quick Check:

    $addToSet + $each avoids duplicates [OK]
Hint: Use $addToSet with $each to add unique items [OK]
Common Mistakes:
  • Using $push which allows duplicates
  • Not using $each with $addToSet
  • Passing array directly without $each
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard