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 a specific array element using the positional $ operator
Given the collection
Which update query will change the first grade equal to 90 to 95?
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 } })Attempts:
2 left
💡 Hint
The positional $ operator requires the query to match the array element directly.
✗ Incorrect
Option B correctly matches the document with _id 1 and an array element grades equal to 90, then uses the positional $ operator to update that element to 95. Option B lacks the array element match, so $ cannot identify which element to update. Option B uses $elemMatch incorrectly with a direct value. Option B will not match the document because grades: {$eq: 90} requires the entire grades field to equal 90.
❓ query_result
intermediate2:00remaining
Result of updating multiple array elements with positional $ operator
Consider the document:
What will be the value of
{ "_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 } })?Attempts:
2 left
💡 Hint
The positional $ operator updates only the first matching array element.
✗ Incorrect
The query matches the first element in tasks where done is false (task1). The update sets that element's done to true. The second element remains unchanged. So only task1.done becomes true.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
The positional $ operator cannot be used inside the query filter like this.
✗ Incorrect
Option A uses "items.$" in the query filter, which is invalid syntax. The positional $ operator is only valid in the update document, not in the query filter. Options A, B, and D are syntactically valid.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Use the positional $ operator with a filter that matches the array element.
✗ Incorrect
Option C updates only one document matching userId 123 and the first task with done false, setting its status to completed. Option C updates multiple documents and uses arrayFilters which is more complex and affects multiple elements. Option C lacks the array element match in the query, so $ cannot identify which element to update. Option C updates multiple documents but $ operator only updates the first matching element per document, which may be inefficient if only one update is needed.
🧠 Conceptual
expert2:00remaining
Understanding limitations of the positional $ operator
Which statement about the positional $ operator in MongoDB updates is TRUE?
Attempts:
2 left
💡 Hint
Think about how MongoDB identifies which array element to update.
✗ Incorrect
Option A is true: the positional $ operator updates the first array element matched by the query filter. Option A is false because $ updates only one element per matched document. Option A is false because $ is used in update documents, not aggregation pipelines. Option A is false because the query filter must include the array field to identify the element to update.