0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.