Tutorials Logic, IN info@tutorialslogic.com

MongoDB Update Operators $set, $push, $inc

Atomic Document Updates

MongoDB update operators modify selected fields without replacing the complete document. $set assigns a value, $unset removes a field, $inc changes a numeric value, and array operators modify array membership. A single-document update is atomic, but a read followed by a separate write can still race.

Change Without Replacing

Updating a user display name should not overwrite preferences, addresses, or audit fields. Operators such as $set and $inc avoid whole-document replacement.

  • $set changes specific fields.
  • $unset removes fields.
  • $inc handles atomic counters.

Array Updates

Arrays need different operators depending on intent. Appending, preventing duplicates, removing values, and updating embedded items are separate operations.

  • Use $push to append.
  • Use $addToSet to avoid duplicates.
  • Use $pull to remove matching values.
  • Use arrayFilters for targeted embedded updates.

Upserts and Safety

Upsert is useful for idempotent writes, but dangerous with vague filters. Use a unique key in the filter and $setOnInsert for creation-only fields.

  • Check matchedCount and modifiedCount.
  • Run countDocuments before large updateMany operations.
  • Use transactions when several collections must change together.

Precise Update Filters

Include identity and any concurrency condition in the update filter, then inspect matchedCount and modifiedCount. Dot notation targets nested fields without replacing the entire parent object. Validate operator and field choices on the server instead of accepting an arbitrary update document from a client.

$push can create duplicates while $addToSet enforces set-like membership for exact values. Use arrayFilters for selected nested array elements and test the filter carefully. Multi-document consistency requires a transaction only when the data model cannot keep the invariant in one document.

Inventory Reservation Update

Inventory Reservation Update
db.products.updateOne(
  { sku: "KB-101", stock: { $gte: 1 } },
  { $inc: { stock: -1, reserved: 1 }, $currentDate: { updatedAt: true } }
)

Update One Matching Array Element

An array filter changes the intended line item without replacing the full array.

Update One Matching Array Element
db.orders.updateOne(
  { _id: 42 },
  { $set: { 'items.$[item].status': 'packed' } },
  { arrayFilters: [{ 'item.sku': 'BK-7', 'item.status': 'paid' }] }
)
Output
Only the paid BK-7 item becomes packed.
  • Keep the document filter and array filter precise to avoid a broad accidental update.
Before you move on

MongoDB Update Operators $set, $push, $inc Mastery Check

4 checks
  • Choose the correct update operator for the intended change.
  • Protect updates with specific filters.
  • Understand upsert creation behavior.
  • Review matched and modified counts.

MongoDB Update Operators Questions Learners Ask

No. The filter chooses documents; update operators define the change.

$inc is atomic for a single document and avoids read-modify-write races.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.