$each modifier with $push in MongoDB - Time & Space Complexity
When we add multiple items to an array in MongoDB using $push with $each, it is important to understand how the time taken grows as we add more items.
We want to know how the work changes when the number of items we add increases.
Analyze the time complexity of the following code snippet.
db.collection.updateOne(
{ _id: 1 },
{ $push: { scores: { $each: [85, 90, 95] } } }
)
This code adds multiple scores to the scores array of a document with _id 1 using $push and $each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each item from the
$eacharray to the target array. - How many times: Once for each item in the
$eacharray.
As the number of items in the $each array grows, the number of operations grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions to the array |
| 100 | 100 additions to the array |
| 1000 | 1000 additions to the array |
Pattern observation: The work grows directly with the number of items added.
Time Complexity: O(n)
This means the time to add items grows in a straight line with the number of items you add.
[X] Wrong: "Adding multiple items with $each is done in constant time, no matter how many items there are."
[OK] Correct: Each item must be added one by one, so more items mean more work and more time.
Understanding how batch updates like $push with $each scale helps you reason about database performance and write efficient queries in real projects.
"What if we used $addToSet with $each instead of $push? How would the time complexity change?"
Practice
$each modifier do when used with $push in MongoDB?Solution
Step 1: Understand
$pushusage$pushadds elements to an array field in a document.Step 2: Role of
$eachmodifier$eachallows adding multiple elements at once inside$push, making the update efficient.Final Answer:
It adds multiple values to an array in a single update operation. -> Option BQuick Check:
$eachadds many items [OK]
$each adds many items inside $push [OK]- Thinking
$eachremoves items - Using
$eachoutside$push - Confusing
$eachwith sorting
tags using $push with $each?Solution
Step 1: Check correct
$pushsyntax$pushrequires a field and a value or modifier object.Step 2: Validate
$eachusage$eachmust be an array inside$pushto add multiple items.Final Answer:
{ $push: { tags: { $each: ["a", "b", "c"] } } } -> Option AQuick Check:
Correct$eachsyntax = { $push: { tags: { $each: ["a", "b", "c"] } } } [OK]
$each with an array inside $push [OK]- Passing array directly without
$each - Using invalid operator like
$add - Incorrect
$eachformat without array brackets
{ _id: 1, colors: ["red"] }, what will be the colors array after running this update?{ $push: { colors: { $each: ["blue", "green"] } } }Solution
Step 1: Initial array and update operation
Original array is ["red"]. The update uses$pushwith$eachto add "blue" and "green".Step 2: Effect of
$eachinside$push$eachadds each element individually, so "blue" and "green" are appended to the array.Final Answer:
["red", "blue", "green"] -> Option CQuick Check:
Array after update = ["red", "blue", "green"] [OK]
$each adds items individually, not as a nested array [OK]- Expecting nested array inside colors
- Replacing array instead of pushing
- Ignoring original array elements
{ $push: { items: { $each: "apple", "banana" } } }Solution
Step 1: Analyze
$eachvalue type$eachrequires an array of values, but here it is given as separate strings without array brackets.Step 2: Identify syntax error
Passing multiple strings without array brackets causes a syntax error in MongoDB update.Final Answer:
The$eachvalue must be an array, not separate strings. -> Option AQuick Check:
$eachneeds array brackets [OK]
$each values in square brackets [OK]- Passing multiple values without array brackets
- Confusing
$eachwith$addToSet - Assuming
$pushalone can add multiple items
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?Solution
Step 1: Understand requirement to avoid duplicates
$pushadds items regardless of duplicates, so it doesn't fit the requirement.Step 2: Use
$addToSetwith$each$addToSetadds unique values only, and supports$eachto add multiple items without duplicates.Final Answer:
{ $addToSet: { tags: { $each: ["urgent", "review", "backend"] } } } -> Option DQuick Check:
$addToSet + $eachavoids duplicates [OK]
$addToSet with $each to add unique items [OK]- Using
$pushwhich allows duplicates - Not using
$eachwith$addToSet - Passing array directly without
$each
Ready to prove it?
Pass the test and this pattern moves unknown
