MongoDB Update Operators and Array Patterns/Update the first matching array element

Use the positional `$` operator.

Section: Arrays and positional updates

Update the first matching array element

javascript
javascript
db.orders.updateOne({ _id: 1, "items.sku": "A100" }, { $set: { "items.$.qty": 3 } })
Explanation

The positional operator targets the first matched array element in the query.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Arrays and positional updates
Append to an array
Push one item onto an array field.
OpenIn sheetjavascriptsame section
Append only if missing
Use `$addToSet` to avoid duplicates.
OpenIn sheetjavascriptsame section
Remove array values
Delete matching values from an array.
OpenIn sheetjavascriptsame section
Target specific nested elements with arrayFilters
Update matching array elements with named filters.
OpenIn sheetjavascriptsame section
Increment a numeric field
Increase a counter with `$inc`.
OpenIn sheetjavascript1 tag match
Remove a field
Delete one or more fields from matched documents.
OpenIn sheetjavascript1 tag match