Recall & Review
beginner
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?✗ 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?
✗ 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?✗ Incorrect
Use $inc with $[] to add 10 to every element in the array.
True or False:
$[] requires a filter condition to work.✗ Incorrect
$[] updates all elements without needing a filter condition.
What happens if you use
{ $set: { "items.$[]": null } } in an update?✗ 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.