What does the $[] operator do in a MongoDB update?
The $[] operator updates all elements in an array. It applies the update to every item inside the array.
Click to reveal answer
beginner
How do you update every element in an array field called scores by adding 5 to each score using $[]?
Use { $inc: { "scores.$[]": 5 } } in the update command. This adds 5 to every element inside the scores array.
Click to reveal answer
intermediate
Can $[] be used without a filter condition inside the array?
Yes. $[] updates all elements in the array regardless of their value or condition. It is an all positional operator.
Click to reveal answer
intermediate
What is the difference between $[] and $[<identifier>] in MongoDB updates?
$[] updates all elements in the array. $[<identifier>] updates only elements that match a specified filter condition.
Click to reveal answer
beginner
Write a MongoDB update query to set all elements in the array tags to the string "updated".
Use { $set: { "tags.$[]": "updated" } } in the update command. This replaces every element in the tags array with "updated".
Click to reveal answer
What does the $[] operator update in MongoDB?
AOnly elements matching a filter
BOnly the first element in an array
CAll elements in an array
DOnly the last element in an array
✗ Incorrect
$[] updates all elements in the array, applying the update to each one.
Which update operator would you use to update only specific elements in an array based on a condition?
A$[<identifier>]
B$inc
C$[]
D$set
✗ Incorrect
$[] lets you specify a filter to update only matching array elements.
How would you add 10 to every number in an array field named values?
A{ $push: { "values": 10 } }
B{ $set: { "values.$[]": 10 } }
C{ $inc: { "values": 10 } }
D{ $inc: { "values.$[]": 10 } }
✗ Incorrect
Use $inc with $[] to add 10 to every element in the array.
True or False: $[] requires a filter condition to work.
ATrue
BFalse
COnly for numeric arrays
DOnly for string arrays
✗ Incorrect
$[] updates all elements without needing a filter condition.
What happens if you use { $set: { "items.$[]": null } } in an update?
AAll elements in the <code>items</code> array become <code>null</code>
BOnly the first element becomes <code>null</code>
CThe entire <code>items</code> array is removed
DNo change happens
✗ Incorrect
All elements in the items array are set to null using $[].
Explain how the $[] operator works in MongoDB array updates and give an example.
Think about updating every item inside an array at once.
You got /3 concepts.
Describe the difference between $[] and $[] in MongoDB updates.
One updates all, the other updates some based on a condition.
You got /3 concepts.
Practice
(1/5)
1. What does the $[] operator do in a MongoDB update query?
easy
A. It updates every element in an array inside the matched documents.
B. It updates only the first element of an array.
C. It removes all elements from an array.
D. It adds a new element to the array.
Solution
Step 1: Understand the role of $[]
The $[] operator is called the all positional operator and targets all elements in an array during an update.
Step 2: Compare with other array update operators
Unlike $ which updates the first matched element, $[] updates every element in the array.
Final Answer:
It updates every element in an array inside the matched documents. -> Option A
Quick Check:
$[] updates all array elements [OK]
Hint: Remember $[] means update all items in the array [OK]
Common Mistakes:
Confusing $[] with $ which updates only one element
Thinking $[] adds or removes elements
Assuming $[] works outside arrays
2. Which of the following is the correct syntax to increment every element in the scores array by 1 using $[] in MongoDB?
easy
A. db.collection.updateMany({}, { $inc: { 'scores.$[]': 1 } })
B. db.collection.updateMany({}, { $set: { 'scores.$[]': 1 } })
C. db.collection.updateMany({}, { $push: { 'scores.$[]': 1 } })
D. db.collection.updateMany({}, { $inc: { 'scores': 1 } })
Solution
Step 1: Identify the operator to increment array elements
The $inc operator increments numeric values. Using 'scores.$[]' targets all elements in the array.
Step 2: Check syntax correctness
db.collection.updateMany({}, { $inc: { 'scores.$[]': 1 } }) correctly uses $inc with $[] to increment each element by 1. Other options either use wrong operators or wrong field paths.
Hint: Only $inc, $mul, $set work with $[], not $push [OK]
Common Mistakes:
Using $push with $[] causing syntax error
Confusing $inc and $mul usage
Trying to multiply array directly without $[]
5. You have documents with a field items as an array of objects like { _id: 1, items: [{qty: 2}, {qty: 5}] }. You want to add 3 to the qty of every item. Which update query correctly does this?