Chapter 11: MongoDB Update Operators

MongoDB Update Operators — the $ prefixed tools you put in the second argument of updateOne(), updateMany(), findOneAndUpdate(), replaceOne() (sometimes), and even aggregation $merge/$set stages.

These operators are what make updates atomic, efficient, and expressive — MongoDB modifies only what you ask for, without you having to read → change → write the whole document yourself.

1. Big Picture – Categories of Update Operators (2026)

MongoDB groups them roughly like this (from official docs):

Category Purpose Most popular operators Daily usage frequency
Field Update Change / create / remove scalar fields $set, $unset, $inc, $mul, $rename, $currentDate, $min, $max ★★★★★ (almost every update)
Array Update Manipulate arrays (append, remove, reorder, etc.) $push, $pull, $addToSet, $pop, $pullAll ★★★★☆ (very common)
Array Filters / Positional Target specific array elements during update $[<identifier>], $[], $[<condition>] ★★★☆☆ (advanced but powerful)
Bitwise Bit-level operations on integers $bit ★☆☆☆☆ (rare)
Others Less common (e.g., bulkWrite helpers)

Key rules teacher always repeats:

  • Update operators must be inside an object like { $set: {…}, $inc: {…} }
  • You can combine multiple operators in one update (they apply in order)
  • Fields are processed in lexicographic order (since MongoDB 5.0+), but numeric dotted paths sort numerically
  • Use dot notation for nested fields: “profile.age”: 25

2. Hands-on – Most Important Update Operators (with examples)

Let’s use our familiar school2026 database again:

JavaScript

A. Field Update Operators (Daily Bread)

  1. $set — Set / create field (most used operator ever)
JavaScript
  1. $unset — Remove field completely
JavaScript
  1. $inc — Increment / decrement numbers
JavaScript
  1. $currentDate — Set to now (very useful for timestamps)
JavaScript
  1. $rename — Change field name
JavaScript
  1. $min / $max — Set only if smaller/larger
JavaScript

B. Array Update Operators (Where MongoDB shines)

  1. $push — Append to array
JavaScript

With modifiers (very powerful):

JavaScript
  1. $addToSet — Add only if not already present
JavaScript
  1. $pull — Remove matching elements
JavaScript
JavaScript
  1. $pop — Remove first/last element
JavaScript

C. Positional / Filtered Positional Updates (Advanced but very useful)

JavaScript

3. Quick Summary Table – Your Go-To Cheat Sheet

Operator What it does (one-liner) Typical Example Use Case Combine with others?
$set Set value (create if missing) Update name, status, nested score Yes
$unset Remove field Clean old/temporary fields Yes
$inc + / – number Likes, views, points, counters Yes
$push Append to array Add comment, tag, log entry Yes (with $each)
$addToSet Append only if unique Unique friends, categories, roles Yes
$pull Remove from array Delete tag, remove invalid entry Yes
$currentDate Set to current date/timestamp updatedAt, lastLogin Yes
$rename Rename field Fix naming mistakes Yes
$min / $max Keep smallest / largest value Track min/max price, score Yes

4. Mini Exercise – Try Right Now in mongosh!

  1. Give +10 likes and update lastActive to now for Rahul
  2. Add “mongodb” to Rahul’s hobbies only if not present
  3. Remove “cricket” from hobbies of all students
  4. Rename marks.math → marks.mathematics everywhere
  5. Check results with find().pretty()

Understood beta? These operators are what make MongoDB updates feel surgical — precise, safe, and fast.

Next class — what do you want?

  • Deep dive into arrayFilters + positional $ operators?
  • bulkWrite() for mixed insert/update/delete?
  • findOneAndUpdate() (return before/after)?
  • Or start a small project combining all CRUD + operators?

Tell me — we’re building real skills here! 🚀❤️

Any operator still confusing? Ask right away — we’ll practice together! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *