Learn › MongoDB fundamentals
Updating documents
$set, $inc, upsert, replaceOne — and the update that silently deletes fields.
By the end: Change existing documents without destroying the parts you did not mean to touch.
Lessons
- update() and $set
MongoDB $set, and the update that silently deletes every other field in your document. The most expensive beginner mistake.
- $inc and counters
MongoDB $inc for atomic counters: increment and decrement a numeric field without reading it first.
- Atomicity: why $inc beats read-modify-write
Single-document writes in MongoDB are atomic. What that guarantee covers, what it does not, and how to use it correctly.
- updateOne vs updateMany
updateOne vs updateMany in MongoDB: which documents each one touches, and why updateOne is the safer default.
- replaceOne vs $set
replaceOne vs $set in MongoDB: one swaps the whole document, the other edits fields. Knowing which you called matters.
- Upsert: insert or update
MongoDB upsert: update a document if it exists, insert it if it does not, in a single atomic operation.