Setting the file. One moment.
Skill 05 · MongoDB Query Optimizer
Subchapter 5.4
references/update-query-examples.mdMarkdown1 KBView on GitHub
Bad — Full document replacement generates a large oplog entry:
db.coll.replaceOne({ _id: X }, entireNewDocument)Good — Use aggregation-based update to generate smaller oplog deltas:
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.
Bad — Using findOneAndUpdate when you don’t need the document returned:
db.coll.findOneAndUpdate(
{ _id: X },
{ $set: { status: "processed" } }
)Good — Use updateOne when you don’t need the result document:
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.