The $push operator adds a new value to an array field in a document. If the field doesn't exist, it creates the array with the new value.
Click to reveal answer
beginner
What is the purpose of the $slice modifier when used with $push?
The $slice modifier limits the size of the array after pushing a new element. It keeps only a specified number of elements from the start or end of the array.
Click to reveal answer
intermediate
How do you keep only the last 3 elements of an array when pushing a new element using $push and $slice?
Use $push with $each to add the element, and $slice: -3 to keep the last 3 elements. Example: { $push: { arr: { $each: [newValue], $slice: -3 } } }
Click to reveal answer
intermediate
What happens if you use $slice: 0 with $push?
Using $slice: 0 removes all elements from the array after pushing, resulting in an empty array.
Click to reveal answer
beginner
Can $slice be used alone without $push in an update operation?
No, $slice is a modifier used only with $push to control the array size after pushing elements. It cannot be used alone.
Click to reveal answer
What does $push do in MongoDB?
ARemoves a value from an array
BAdds a value to an array field
CReplaces the entire document
DCreates a new collection
✗ Incorrect
$push adds a new element to an array field in a document.
How does $slice: -3 affect an array when used with $push?
ARemoves all elements
BKeeps the first 3 elements
CKeeps the last 3 elements
DAdds 3 new elements
✗ Incorrect
$slice: -3 keeps only the last 3 elements of the array after pushing.
Which of these is the correct syntax to push a value and limit array size to 5?
3. Given the document { _id: 1, items: [1, 2, 3, 4] }, what will be the items array after running this update? db.collection.updateOne({ _id: 1 }, { $push: { items: { $each: [5, 6], $slice: -4 } } })
medium
A. [3, 4, 5, 6]
B. [1, 2, 3, 4, 5, 6]
C. [2, 3, 4, 5]
D. [5, 6]
Solution
Step 1: Understand $each and $slice with negative value
$each adds [5, 6] to the array. $slice: -4 keeps the last 4 elements of the array.
Step 2: Calculate new array content
Original array: [1, 2, 3, 4] After push: [1, 2, 3, 4, 5, 6] Apply $slice: -4: keep last 4 elements -> [3, 4, 5, 6]
Final Answer:
[3, 4, 5, 6] -> Option A
Quick Check:
Negative $slice keeps last elements [OK]
Hint: Negative $slice keeps last N elements [OK]
Common Mistakes:
Ignoring $slice and keeping all elements
Using positive $slice logic incorrectly
Removing new elements instead of old
4. You run this update: db.collection.updateOne({ _id: 2 }, { $push: { logs: { $each: ["a", "b"], $slice: 1 } } }) But the array logs ends up with 1 element that is an old one instead of a newly pushed one. What is the likely mistake?
medium
A. Not specifying $position causes elements to append incorrectly
B. Missing $each causes syntax error
C. Using $slice with a negative number causes array to clear
D. Using positive $slice keeps first elements instead of newest ones
Solution
Step 1: Understand positive $slice behavior
Positive $slice: 1 keeps only the first element of the array after push.
Step 2: Why old elements remain
Positive $slice: 1 keeps the first (oldest) element of the resulting array since new elements are appended to the end.
Step 3: Common confusion
Positive $slice keeps elements from the start, so if you want to keep the latest elements, use negative $slice.
Final Answer:
Using positive $slice keeps first elements instead of newest ones -> Option D
Quick Check:
Positive $slice keeps start elements [OK]
Hint: Use negative $slice to keep newest elements [OK]
Common Mistakes:
Confusing positive and negative $slice behavior
Omitting $each inside $push
Expecting $slice to remove elements before push
5. You want to keep only the 5 most recent messages in a chat array after adding new messages ["msg4", "msg5"]. The current array is ["msg1", "msg2", "msg3", "msg4"]. Which update query correctly achieves this?