← Files MongoDB AtlasARCHIVED FILE
references/update-query-examples.md
1.12 KB · Sep 30, 2026 · 22:47 UTC
# Update Query Examples
## replaceOne vs. updateOne with $replaceWith
**Bad** — Full document replacement generates a large oplog entry:
```javascript
db.coll.replaceOne({ _id: X }, entireNewDocument)
```
**Good** — Use aggregation-based update to generate smaller oplog deltas:
```javascript
db.coll.updateOne({ _id: X }, [{ $replaceWith: { $literal: entireNewDocument } }])
```
**Why:** `replaceOne` writes the full document to the oplog. The aggregation update syntax lets MongoDB compute deltas, resulting in smaller oplog entries when only a few fields are changed.
## findOneAndUpdate Misuse vs. updateOne
**Bad** — Using `findOneAndUpdate` when you don't need the document returned:
```javascript
db.coll.findOneAndUpdate(
{ _id: X },
{ $set: { status: "processed" } }
)
```
**Good** — Use `updateOne` when you don't need the result document:
```javascript
db.coll.updateOne(
{ _id: X },
{ $set: { status: "processed" } }
)
```
**Why:** `findOneAndUpdate` writes a copy of the pre-change document to a side collection for retryable writes. This overhead is unnecessary if you don't need the returned document.SHA-256: ddc421ffa5b11c57fa6bac9e81bf57e8ceddb34813995d39e1629bc891a759ff