1.Single-document writes are atomic
This is the foundation the rest of this section builds on: a single write to a single document always applies completely or not at all, and no concurrent reader ever observes it half-done. Every concurrency pattern here — atomic operators, optimistic concurrency, compare-and-set — leans on this guarantee already existing.
Key features
- Any single write call — updateOne, findOneAndUpdate, replaceOne — is atomic for the one document it targets
- This is a per-write-call guarantee, not a per-logical-operation one — reading a value and then writing based on it is two operations, not one
- It holds regardless of how many fields, nested objects, or array elements the update touches
- It does not, by itself, prevent a "lost update" from two concurrent read-then-write sequences racing each other
- Everything else in this section is a pattern for the gap this guarantee leaves: coordinating a decision based on a document's current value
Command
// Given: one write call to one document is always atomic. Ask: does my operation actually fit inside one call?A single write call to one document is always atomic — but a read, then a decision, then a separate write is not one atomic operation, however fast it looks in testing.