MongoDB Update Operators

MongoDB Update Operators ✏️

In MongoDB, update operators are special keywords (starting with $) used with updateOne(), updateMany(), and findOneAndUpdate() to modify documents without replacing them entirely.

1️⃣ Field Update Operators

$set – Set or change field value



 

$unset – Remove a field



 


2️⃣ Numeric Update Operators

$inc – Increment / Decrement value



 

$mul – Multiply value



 


3️⃣ Array Update Operators

$push – Add element to array



 

$pull – Remove element from array



 

$addToSet – Add unique value only



 


4️⃣ Rename Operator

$rename – Rename a field



 


5️⃣ Date Operators

$currentDate – Set current date/time



 


6️⃣ Bitwise Operator

$bit – Bitwise update



 


7️⃣ Replace Operator (replaceOne) ⚠️

Replaces the entire document (except _id):



 


8️⃣ Upsert with Update Operators



 


SQL vs MongoDB (UPDATE)

SQL



 

MongoDB



 


Common Mistakes ⚠️

  • Forgetting $set (overwrites document)

  • Using replaceOne() accidentally

  • Pushing duplicate values instead of $addToSet


Quick Summary 🧠



 

You may also like...