Single-document writes are atomic
standardintermediateThis 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.
Think of it as
Treat this as the one free concurrency primitive MongoDB gives every write, then ask what pattern gets you the rest of the way for anything that guarantee alone does not cover, like "read a value, then write based on what you read" — a sequence that atomicity alone does not protect.
What we're doing: Show that atomicity covers a single call but not a read-then-decide-then-write sequence.
- 2
- A pure increment needs no prior read — one atomic call does the whole job.
- 5
- Between the findOne and the updateOne, another concurrent request could also read stock > 0 and also decrement — both proceed thinking they safely checked, and stock can go negative.
Why this works: The read-check-write sequence is where single-document atomicity stops helping — it covers each individual call, but says nothing about what happens between two calls, which is exactly the gap the rest of this section's patterns exist to close.
Remember: 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.
See also: single document atomicity · atomic operators over read modify write

