Challenge - 5 Problems
Array Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Update all array elements with $[] operator
Given the collection
Which update query will change all sizes to "XL" using the
products with documents like:{ "_id": 1, "sizes": ["S", "M", "L"] }Which update query will change all sizes to "XL" using the
$[] all positional operator?MongoDB
db.products.updateMany({}, { $set: { "sizes.$[]": "XL" } })Attempts:
2 left
💡 Hint
Use $[] to update all elements in an array to the same value.
✗ Incorrect
The $[] operator targets all elements in the array. Setting "sizes.$[]" to "XL" changes every element to "XL". The other options either misuse $[] or try unsupported expressions.
❓ query_result
intermediate2:00remaining
Effect of $[] on nested arrays
Consider documents:
Which update query will add 10 to every number inside all nested arrays using $[]?
{ "_id": 1, "matrix": [[1,2], [3,4]] }Which update query will add 10 to every number inside all nested arrays using $[]?
MongoDB
db.collection.updateMany({}, { $inc: { "matrix.$[].$[]": 10 } })Attempts:
2 left
💡 Hint
Use $[] for each level of nested arrays.
✗ Incorrect
To update all numbers inside nested arrays, you must use $[] twice: once for the outer array and once for the inner arrays. Option D correctly uses "matrix.$[].$[]" to target each number.
📝 Syntax
advanced2:00remaining
Identify the syntax error in $[] update
Which option contains a syntax error when trying to update all elements in array
tags to lowercase?Attempts:
2 left
💡 Hint
Array field references inside aggregation expressions must be valid.
✗ Incorrect
Option A uses "$tags[]" which is invalid syntax for field path references. The others use valid field path references with $[] or $.
❓ optimization
advanced2:00remaining
Optimizing update of array elements with $[]
You want to set all elements in the
scores array to 100 only if they are less than 100. Which update query is the most efficient?Attempts:
2 left
💡 Hint
Use arrayFilters to target only elements matching a condition.
✗ Incorrect
Option C uses $[elem] with arrayFilters to update only elements less than 100, avoiding unnecessary writes. Options B and C update all elements or documents, and D uses unsupported syntax in update.
🧠 Conceptual
expert3:00remaining
Understanding $[] behavior with multiple arrays
Given documents:
Which update query will increment every element in both
{ "_id": 1, "arr1": [1,2], "arr2": [3,4] }Which update query will increment every element in both
arr1 and arr2 by 1 using $[]?Attempts:
2 left
💡 Hint
Use $[] for each array field separately.
✗ Incorrect
Option A correctly uses $[] for each array field to increment all elements. Option A is invalid syntax. Option A updates only one matched element per array. Option A tries to increment arrays directly, causing error.