Bird
Raised Fist0
MongoDBquery~20 mins

Array update with positional $ operator 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 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.

Practice

(1/5)
1.

What does the positional $ operator do in a MongoDB update?

easy
A. It updates the first matching element in an array field.
B. It deletes the entire array field.
C. It adds a new element to the array.
D. It replaces the whole document.

Solution

  1. Step 1: Understand the positional operator usage

    The $ operator targets the first array element that matches the query condition.
  2. Step 2: Recognize update effect

    It updates only that matched element without changing other array elements or the whole array.
  3. Final Answer:

    It updates the first matching element in an array field. -> Option A
  4. Quick Check:

    Positional $ updates one array item [OK]
Hint: Positional $ updates only the first matched array element [OK]
Common Mistakes:
  • Thinking $ replaces the whole array
  • Confusing $ with $push or $pop
  • Assuming it updates all matching elements
2.

Which of the following is the correct syntax to update the first matching element in the scores array where score is 85?

db.students.updateOne(
  { "scores.score": 85 },
  { $set: { "scores.$.score": 90 } }
)
easy
A. db.students.updateOne({ "scores.score": 85 }, { $set: { "scores.score": 90 } })
B. db.students.updateOne({ "scores.$.score": 85 }, { $set: { "scores.$.score": 90 } })
C. db.students.updateOne({ "scores.score": 85 }, { $set: { "scores.$.score": 90 } })
D. db.students.updateOne({ "scores.$.score": 85 }, { $set: { "scores.score": 90 } })

Solution

  1. Step 1: Identify the query filter

    The filter must be on the array field without the positional operator: { "scores.score": 85 }.
  2. Step 2: Use positional operator in update

    The update uses "scores.$.score" to update the matched element's score.
  3. Final Answer:

    db.students.updateOne({ "scores.score": 85 }, { $set: { "scores.$.score": 90 } }) -> Option C
  4. Quick Check:

    Query without $; update with scores.$ [OK]
Hint: Use $ only in update path, not in query filter [OK]
Common Mistakes:
  • Using $ in the query filter
  • Not using $ in the update path
  • Updating the whole array instead of one element
3.

Given the document:

{ _id: 1, items: [ { name: "apple", qty: 5 }, { name: "banana", qty: 3 } ] }

What will be the document after running this update?

db.collection.updateOne(
  { _id: 1, "items.name": "banana" },
  { $set: { "items.$.qty": 10 } }
)
medium
A. { _id: 1, items: [ { name: "apple", qty: 5 }, { name: "banana", qty: 10 } ] }
B. { _id: 1, items: [ { name: "apple", qty: 10 }, { name: "banana", qty: 3 } ] }
C. { _id: 1, items: [ { name: "apple", qty: 5 }, { name: "banana", qty: 3 }, { name: "banana", qty: 10 } ] }
D. { _id: 1, items: [ { name: "apple", qty: 5 } ] }

Solution

  1. Step 1: Find matching array element

    The query matches document with _id 1 and array element where name is "banana".
  2. Step 2: Update matched element's qty

    The positional operator $ updates the qty of the matched "banana" element to 10.
  3. Final Answer:

    { _id: 1, items: [ { name: "apple", qty: 5 }, { name: "banana", qty: 10 } ] } -> Option A
  4. Quick Check:

    Only matched array element qty changed [OK]
Hint: Positional $ updates only matched array element [OK]
Common Mistakes:
  • Updating wrong array element
  • Adding new element instead of updating
  • Replacing entire array
4.

What is wrong with this update query?

db.orders.updateOne(
  { "products.$.id": 123 },
  { $set: { "products.$.quantity": 5 } }
)
medium
A. Using positional operator $ in the query filter is invalid.
B. Both A and C are correct.
C. The query filter should use products.id without $.
D. The $set operator cannot update array elements.

Solution

  1. Step 1: Check query filter syntax

    The positional operator $ cannot be used in the query filter path; it must be omitted.
  2. Step 2: Correct query filter

    The filter should be { "products.id": 123 } to find the matching array element.
  3. Final Answer:

    Both A and C are correct. -> Option B
  4. Quick Check:

    No $ in query filter; only in update path [OK]
Hint: Never use $ in query filter, only in update path [OK]
Common Mistakes:
  • Using $ in query filter
  • Confusing query filter and update syntax
  • Assuming $set can't update arrays
5.

You have documents like:

{ _id: 1, tags: [ { name: "urgent", status: "open" }, { name: "review", status: "closed" } ] }

Write an update query to change the status of the tag with name "review" to "open" using the positional $ operator.

hard
A. db.collection.updateOne({ "tags.name": "review" }, { $set: { "tags.$.name": "open" } })
B. db.collection.updateOne({ "tags.$.name": "review" }, { $set: { "tags.status": "open" } })
C. db.collection.updateOne({ "tags.name": "review" }, { $set: { "tags.status": "open" } })
D. db.collection.updateOne({ "tags.name": "review" }, { $set: { "tags.$.status": "open" } })

Solution

  1. Step 1: Use correct query filter

    The filter should find the document with an array element where tags.name is "review".
  2. Step 2: Use positional operator in update

    The update uses tags.$.status to update the matched tag's status to "open".
  3. Final Answer:

    db.collection.updateOne({ "tags.name": "review" }, { $set: { "tags.$.status": "open" } }) -> Option D
  4. Quick Check:

    Query on tags.name; update tags.$.status [OK]
Hint: Match array element in query; update with tags.$.field [OK]
Common Mistakes:
  • Using $ in query filter path
  • Updating wrong field in array
  • Not using positional operator in update
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