updateOne() - update a single document
Syntax
Parameters
filter (Object, required)
Filter criteria to match the documents to be updated.
update (Object, required)
For update operations, update operators (such as $set, $inc, etc.) must be used.
Supported update operators:
$set- Set field value$unset- Delete field$inc- Incremental value$mul- multiply the value$push- Add array element$pull- delete array element$addToSet- Add unique array element- Wait...
options (Object, optional)
Operation options.
Return value
Return Promise<UpdateResult> object:
Note:
matchedCountmay be greater thanmodifiedCount(match but do not modify if the values are the same)- When there is no match, both
matchedCountandmodifiedCountare 0
Example
Basic update
Increment counter
Array operations
Multiple operator combinations
Update nested fields
Delete field
Use upsert
Conditional update
Use comments (facilitates log tracking)
Error handling
Common mistakes
Error: Missing update operator
Note: updateOne is used for partial update, and the update operator must be used. If a complete replacement document is required, use replaceOne.
Error: filter parameter is invalid
Performance recommendations
1. Use index optimization filtering
2. Avoid full table scan
3. Batch update using updateMany
Caching behavior
updateOne does not clear query caches by default after a successful modification. Use cache.invalidate or autoInvalidate: true when the write should clear cache:
Note:
- Invalid cache only if
modifiedCount > 0 - Cache invalidation must be configured explicitly; writes do not clear read caches by default
Slow query log
When the operation time exceeds the threshold, slow query logs will be automatically recorded:
Comparison with other methods
Best Practices
1. Always use the update operator
2. Use $setOnInsert with upsert
3. Add comments for easy tracking
4. Verify update results
Related methods
updateMany()- Batch update multiple documentsreplaceOne()- complete replacement of a single documentfindOneAndUpdate()- find and update atomicallyinsertOne()- Insert a single document