MongoDB mongosh Update
MongoDB mongosh – Update Documents ✏️
MongoDB provides powerful update methods with update operators.
1️⃣ Update a Single Document (updateOne)
Syntax
Example
Output
2️⃣ Update Multiple Documents (updateMany)
3️⃣ Update with Multiple Fields
4️⃣ Important Update Operators
$set – Set / change value
$inc – Increment value
$unset – Remove field
5️⃣ Update Arrays
$push – Add element
$pull – Remove element
6️⃣ Replace Document (replaceOne)
⚠️ Replaces the entire document (except _id)
7️⃣ Upsert (Update or Insert)
If document doesn’t exist, it will be inserted.
8️⃣ Update Using Conditions
SQL vs MongoDB (UPDATE)
SQL
MongoDB
Common Mistakes ⚠️
-
Forgetting update operators (
$set,$inc) -
Using
updateOne()whenupdateMany()is needed -
Accidentally replacing full document
