In short
When a customer buys one item, you don't rewrite the whole product. You subtract one from stock. Update operators change just the parts you name, safely and in a single step.
Update operators change parts of a document without replacing it: $set changes fields, $inc adds to numbers, $push and $pull edit arrays and $unset removes fields.

Think of it like this
Editing a register with a pencil
A kirana shop owner doesn't rewrite the whole stock register when one packet sells. They change the count (inc), correct a price (set), add a new supplier to the list (push) or strike one off (pull). Each edit touches only one line.
Watch it happen
One product, several updates
1/5The original document fields.
The big ideas
What you need to know
$set and $unset
$set changes or adds fields. $unset removes them. Other fields stay as they are.
$inc is atomic
$inc adds to a number on the server, so two sales at the same moment both count correctly.
Array operators
$push adds an item, $addToSet adds only if missing, and $pull removes matching items.
upsert
With { upsert: true }, if nothing matches, MongoDB inserts a new document built from the filter and update.
See it in code
Walk through the code
1// A sale: reduce stock only if at least one is left2db.products.updateOne(3 { _id: productId, stock: { $gte: 1 } },4 { $inc: { stock: -1 }, $set: { updatedAt: new Date() } }5);6 7// Add a tag only once, remove another8db.products.updateOne(9 { name: "Steel Bottle" },10 { $addToSet: { tags: "bestseller" }, $pull: { tags: "clearance" } }11);12 13// Mark all old stock as archived14db.products.updateMany(15 { createdAt: { $lt: new Date("2024-01-01") } },16 { $set: { archived: true } }17);1/4Filter by id and require stock of at least 1, so stock can never go negative.
Targeted updates. The filter decides which documents, the operators decide what changes.
Save yourself the pain
Common mistakes
Oops
db.products.updateOne({ _id: id }, { price: 499 })
Do this instead
Use { $set: { price: 499 } }. Modern drivers reject an update without operators, and replaceOne is the method that replaces whole documents.
Oops
Reading stock, subtracting in JavaScript, then saving it back.
Do this instead
Use $inc in one update. Read-then-write loses sales when two requests run at the same time.
Your mission
Run a day at the shop
Quick check
Two customers buy the last unit at the same moment. Which update keeps stock from going below zero?
Go deeper
- 1Use $inc to reduce stock by one when an order is placed.
- 2Add and remove tags from a product with $addToSet and $pull.
- 3Try upsert: true to insert a document if no match exists.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.