0
0
MongoDBquery~20 mins

Array update with positional $ operator 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 a specific array element using the positional $ operator
Given the collection students with documents like:
{ "_id": 1, "name": "Alice", "grades": [85, 90, 78] }

Which update query will change the first grade equal to 90 to 95?
MongoDB
db.students.updateOne({ _id: 1, grades: 90 }, { $set: { "grades.$": 95 } })
Adb.students.updateOne({ _id: 1 }, { $set: { "grades.$": 95 } })
Bdb.students.updateOne({ _id: 1, grades: 90 }, { $set: { "grades.$": 95 } })
Cdb.students.updateOne({ _id: 1, grades: { $elemMatch: { $eq: 90 } } }, { $set: { "grades.$": 95 } })
Ddb.students.updateOne({ _id: 1, grades: { $eq: 90 } }, { $set: { "grades.$": 95 } })
Attempts:
2 left
💡 Hint
The positional $ operator requires the query to match the array element directly.
query_result
intermediate
2:00remaining
Result of updating multiple array elements with positional $ operator
Consider the document:
{ "_id": 2, "tasks": [ { "name": "task1", "done": false }, { "name": "task2", "done": false } ] }

What will be the value of tasks after running:
db.collection.updateOne({ _id: 2, "tasks.done": false }, { $set: { "tasks.$.done": true } })
?
ANo change, update fails
B[ { "name": "task1", "done": true }, { "name": "task2", "done": true } ]
C[ { "name": "task1", "done": false }, { "name": "task2", "done": true } ]
D[ { "name": "task1", "done": true }, { "name": "task2", "done": false } ]
Attempts:
2 left
💡 Hint
The positional $ operator updates only the first matching array element.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in using positional $ operator
Which update query will cause a syntax error when trying to update an array element using the positional $ operator?
Adb.collection.updateOne({ "items.$": 10 }, { $set: { "items.$.value": 20 } })
B)} } 02 :"eulav.$.smeti" { :tes$ { ,} 01 :"eulav.smeti" {(enOetadpu.noitcelloc.bd
Cb.collection.updateOne({ "items.value": 10 }, { $set: { "items.$.value": 20 } })
Ddb.collection.updateOne({ "items.value": 10 }, { $set: { "items.$.value": 20 } })
Attempts:
2 left
💡 Hint
The positional $ operator cannot be used inside the query filter like this.
optimization
advanced
2:00remaining
Optimizing array element update with positional $ operator
You want to update the status field to 'completed' for the first task with done: false in documents where userId is 123. Which query is the most efficient?
Adb.tasks.updateOne({ userId: 123 }, { $set: { "tasks.$.status": "completed" } })
Bdb.tasks.updateMany({ userId: 123 }, { $set: { "tasks.$[elem].status": "completed" } }, { arrayFilters: [ { "elem.done": false } ] })
Cdb.tasks.updateOne({ userId: 123, "tasks.done": false }, { $set: { "tasks.$.status": "completed" } })
Ddb.tasks.updateMany({ userId: 123, "tasks.done": false }, { $set: { "tasks.$.status": "completed" } })
Attempts:
2 left
💡 Hint
Use the positional $ operator with a filter that matches the array element.
🧠 Conceptual
expert
2:00remaining
Understanding limitations of the positional $ operator
Which statement about the positional $ operator in MongoDB updates is TRUE?
AThe positional $ operator requires the query filter to match the array element to update.
BThe positional $ operator can update multiple matching elements in an array at once.
CThe positional $ operator can be used in aggregation pipeline stages to update arrays.
DThe positional $ operator can be used without specifying the array field in the query filter.
Attempts:
2 left
💡 Hint
Think about how MongoDB identifies which array element to update.