The positional $ operator helps you update a specific item inside an array in a document without changing the whole array.
Array update with positional $ operator in MongoDB
db.collection.updateOne(
{ "arrayField.identifier": value },
{ $set: { "arrayField.$.fieldToUpdate": newValue } }
)The query part finds the document with the array item matching the condition.
The $ in arrayField.$ means the first matched array element.
db.orders.updateOne(
{ "items.productId": 123 },
{ $set: { "items.$.quantity": 5 } }
)db.tasks.updateOne(
{ "steps.stepId": 10 },
{ $set: { "steps.$.status": "done" } }
)db.users.updateOne(
{ "contacts.type": "email" },
{ $set: { "contacts.$.value": "newemail@example.com" } }
)This example inserts a document with an array of items. Then it updates the quantity of the item with sku 'def' to 50 using the positional $ operator.
db.inventory.insertOne({
_id: 1,
items: [
{ sku: "abc", qty: 10 },
{ sku: "def", qty: 20 },
{ sku: "xyz", qty: 30 }
]
})
print('Before update:')
printjson(db.inventory.findOne({_id: 1}))
// Update qty of item with sku 'def' to 50
const updateResult = db.inventory.updateOne(
{ "items.sku": "def" },
{ $set: { "items.$.qty": 50 } }
)
print('Update result:')
printjson(updateResult)
print('After update:')
printjson(db.inventory.findOne({_id: 1}))The positional $ operator updates only the first matching array element.
If no array element matches, no update happens.
Use updateOne to update a single document, or updateMany to update multiple documents.
The positional $ operator lets you update one matching item inside an array.
You find the document and array element by a query, then use $set with arrayField.$.
This is useful to change just one part of an array without replacing the whole array.
Practice
What does the positional $ operator do in a MongoDB update?
Solution
Step 1: Understand the positional operator usage
The$operator targets the first array element that matches the query condition.Step 2: Recognize update effect
It updates only that matched element without changing other array elements or the whole array.Final Answer:
It updates the first matching element in an array field. -> Option AQuick Check:
Positional$updates one array item [OK]
- Thinking $ replaces the whole array
- Confusing $ with $push or $pop
- Assuming it updates all matching elements
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 } }
)Solution
Step 1: Identify the query filter
The filter must be on the array field without the positional operator:{ "scores.score": 85 }.Step 2: Use positional operator in update
The update uses"scores.$.score"to update the matched element's score.Final Answer:
db.students.updateOne({ "scores.score": 85 }, { $set: { "scores.$.score": 90 } }) -> Option CQuick Check:
Query without $; update with scores.$ [OK]
- Using $ in the query filter
- Not using $ in the update path
- Updating the whole array instead of one element
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 } }
)Solution
Step 1: Find matching array element
The query matches document with _id 1 and array element where name is "banana".Step 2: Update matched element's qty
The positional operator$updates the qty of the matched "banana" element to 10.Final Answer:
{ _id: 1, items: [ { name: "apple", qty: 5 }, { name: "banana", qty: 10 } ] } -> Option AQuick Check:
Only matched array element qty changed [OK]
- Updating wrong array element
- Adding new element instead of updating
- Replacing entire array
What is wrong with this update query?
db.orders.updateOne(
{ "products.$.id": 123 },
{ $set: { "products.$.quantity": 5 } }
)Solution
Step 1: Check query filter syntax
The positional operator$cannot be used in the query filter path; it must be omitted.Step 2: Correct query filter
The filter should be{ "products.id": 123 }to find the matching array element.Final Answer:
Both A and C are correct. -> Option BQuick Check:
No $ in query filter; only in update path [OK]
- Using $ in query filter
- Confusing query filter and update syntax
- Assuming $set can't update arrays
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.
Solution
Step 1: Use correct query filter
The filter should find the document with an array element wheretags.nameis "review".Step 2: Use positional operator in update
The update usestags.$.statusto update the matched tag's status to "open".Final Answer:
db.collection.updateOne({ "tags.name": "review" }, { $set: { "tags.$.status": "open" } }) -> Option DQuick Check:
Query on tags.name; update tags.$.status [OK]
- Using $ in query filter path
- Updating wrong field in array
- Not using positional operator in update
Ready to prove it?
Pass the test and this pattern moves unknown
