Bird
Raised Fist0
MongoDBquery~20 mins

Array update with $[] all positional in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Update all array elements with $[] operator
Given the collection 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" } })
Adb.products.updateMany({}, { $set: { "sizes.$[]": { $toUpper: "$sizes.$[]" } } })
Bdb.products.updateMany({}, { $set: { "sizes.$[]": "XL" } })
Cdb.products.updateMany({}, { $set: { "sizes.$": "XL" } })
Ddb.products.updateMany({}, { $set: { "sizes.$[elem]": "XL" } })
Attempts:
2 left
💡 Hint
Use $[] to update all elements in an array to the same value.
❓ query_result
intermediate
2:00remaining
Effect of $[] on nested arrays
Consider documents:
{ "_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 } })
Adb.collection.updateMany({}, { $inc: { "matrix": 10 } })
Bdb.collection.updateMany({}, { $inc: { "matrix.$[]": 10 } })
Cdb.collection.updateMany({}, { $inc: { "matrix.$": 10 } })
Ddb.collection.updateMany({}, { $inc: { "matrix.$[].$[]": 10 } })
Attempts:
2 left
💡 Hint
Use $[] for each level of nested arrays.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $[] update
Which option contains a syntax error when trying to update all elements in array tags to lowercase?
Adb.items.updateMany({}, { $set: { "tags.$[]": { $toLower: "$tags[]" } } })
Bdb.items.updateMany({}, { $set: { "tags.$[]": { $toLower: "$tags" } } })
Cdb.items.updateMany({}, { $set: { "tags.$[]": { $toLower: "$tags.$" } } })
Ddb.items.updateMany({}, { $set: { "tags.$[]": { $toLower: "$tags.$[]" } } })
Attempts:
2 left
💡 Hint
Array field references inside aggregation expressions must be valid.
❓ optimization
advanced
2: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?
Adb.students.updateMany({}, { $set: { "scores.$[]": 100 } })
Bdb.students.updateMany({ "scores": { $lt: 100 } }, { $set: { "scores.$[]": 100 } })
Cdb.students.updateMany({}, { $set: { "scores.$[elem]": 100 } }, { arrayFilters: [{ "elem": { $lt: 100 } }] })
Ddb.students.updateMany({}, { $set: { "scores.$[]": { $cond: [{ $lt: ["$$this", 100] }, 100, "$$this"] } } })
Attempts:
2 left
💡 Hint
Use arrayFilters to target only elements matching a condition.
🧠 Conceptual
expert
3:00remaining
Understanding $[] behavior with multiple arrays
Given documents:
{ "_id": 1, "arr1": [1,2], "arr2": [3,4] }

Which update query will increment every element in both arr1 and arr2 by 1 using $[]?
Adb.collection.updateMany({}, { $inc: { "arr1.$[]": 1, "arr2.$[]": 1 } })
Bdb.collection.updateMany({}, { $inc: { "arr1.$": 1, "arr2.$": 1 } })
Cdb.collection.updateMany({}, { $inc: { "$[]": 1 } })
Ddb.collection.updateMany({}, { $inc: { "arr1": 1, "arr2": 1 } })
Attempts:
2 left
💡 Hint
Use $[] for each array field separately.

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

  1. Step 1: Understand the role of $[]

    The $[] operator is called the all positional operator and targets all elements in an array during an update.
  2. Step 2: Compare with other array update operators

    Unlike $ which updates the first matched element, $[] updates every element in the array.
  3. Final Answer:

    It updates every element in an array inside the matched documents. -> Option A
  4. 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

  1. Step 1: Identify the operator to increment array elements

    The $inc operator increments numeric values. Using 'scores.$[]' targets all elements in the array.
  2. 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.
  3. Final Answer:

    db.collection.updateMany({}, { $inc: { 'scores.$[]': 1 } }) -> Option A
  4. Quick Check:

    Use $inc with 'array.$[]' to increment all elements [OK]
Hint: Use $inc with 'array.$[]' to update all array elements [OK]
Common Mistakes:
  • Using $set instead of $inc for increment
  • Trying to increment the whole array instead of elements
  • Using $push which adds elements instead of updating
3. Given the document { _id: 1, scores: [10, 20, 30] }, what will be the scores array after running this update?
db.collection.updateOne({_id:1}, { $inc: { 'scores.$[]': 5 } })
medium
A. [10, 25, 30]
B. [10, 20, 30]
C. [5, 5, 5]
D. [15, 25, 35]

Solution

  1. Step 1: Understand the update operation

    The update uses $inc with 'scores.$[]', which increments every element in the scores array by 5.
  2. Step 2: Calculate the new array values

    Original array: [10, 20, 30]. After increment: [10+5, 20+5, 30+5] = [15, 25, 35].
  3. Final Answer:

    [15, 25, 35] -> Option D
  4. Quick Check:

    Increment all array elements by 5 = [15, 25, 35] [OK]
Hint: Add increment to each element using 'array.$[]' [OK]
Common Mistakes:
  • Thinking only first element updates
  • Replacing array with single value
  • Not applying increment to all elements
4. You want to update all elements in the ratings array by multiplying each by 2. Which update query will cause an error?
medium
A. db.collection.updateMany({}, { $set: { 'ratings.$[]': 2 } })
B. db.collection.updateMany({}, { $inc: { 'ratings.$[]': 2 } })
C. db.collection.updateMany({}, { $push: { 'ratings.$[]': 2 } })
D. db.collection.updateMany({}, { $mul: { 'ratings.$[]': 2 } })

Solution

  1. Step 1: Analyze each update operator with $[]

    $mul, $inc, and $set can work with $[] to update array elements. $push is for adding elements, not updating existing ones.
  2. Step 2: Identify the invalid usage

    Using $push with $[] is invalid and causes an error because $push expects a field name without positional operators.
  3. Final Answer:

    db.collection.updateMany({}, { $push: { 'ratings.$[]': 2 } }) -> Option C
  4. Quick Check:

    $push cannot be used with $[] [OK]
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?
hard
A. db.collection.updateMany({}, { $set: { items: { $map: { input: "$items", as: "item", in: { qty: { $add: ["$$item.qty", 3] } } } } } })
B. db.collection.updateMany({}, { $inc: { 'items.$[].qty': 3 } })
C. db.collection.updateMany({}, { $set: { 'items.$[].qty': { $add: ['items.$[].qty', 3] } } })
D. db.collection.updateMany({}, { $push: { 'items.$[].qty': 3 } })

Solution

  1. Step 1: Understand the structure and goal

    Each document has an array items with objects containing qty. We want to add 3 to each qty.
  2. Step 2: Check update options

    db.collection.updateMany({}, { $inc: { 'items.$[].qty': 3 } }) uses $inc with items.$[].qty, which correctly increments the qty field of every object in the array. db.collection.updateMany({}, { $set: { items: { $map: { input: "$items", as: "item", in: { qty: { $add: ["$$item.qty", 3] } } } } } }) attempts to use aggregation operators like $map in a simple update, which requires pipeline syntax (array of stages) and is invalid. db.collection.updateMany({}, { $set: { 'items.$[].qty': { $add: ['items.$[].qty', 3] } } }) is invalid syntax. db.collection.updateMany({}, { $push: { 'items.$[].qty': 3 } }) misuses $push.
  3. Final Answer:

    db.collection.updateMany({}, { $inc: { 'items.$[].qty': 3 } }) -> Option B
  4. Quick Check:

    Use $inc with 'array.$[].field' to update nested fields [OK]
Hint: Use $inc with 'array.$[].field' to update nested fields [OK]
Common Mistakes:
  • Using $push to update fields inside objects
  • Trying to use $set with $add inside update document
  • Not using $[] to target all array elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard