Loading

Quipoin Menu

Learn • Practice • Grow

mongodb / Update Operations
tutorial

Update Operations

Data changes over time. A user updates their email, a product price changes, a blog post gets edited. MongoDB provides powerful update operations to modify existing documents. Let's learn how to use `updateOne()`, `updateMany()`, and `replaceOne()`.

Update Operators

Just like query operators help you find documents, **update operators** help you modify them. The most common is `$set`, but there are many others.

OperatorDescription
`$set`Sets the value of a field
`$unset`Removes a field
`$inc`Increments a numeric value
`$mul`Multiplies a numeric value
`$rename`Renames a field
`$push`Adds an element to an array
`$pull`Removes an element from an array

Think of update operators as editing tools. `$set` is like a pen that writes or changes text, `$unset` is like an eraser, and `$inc` is like a counter that ticks up or down.

updateOne() - Update First Matching Document

`updateOne()` updates the first document that matches the filter.
<!-- Update John's age to 31 -->
db.users.updateOne(
  { name: "John" },
  { $set: { age: 31 } }
)

Output:
{
  acknowledged: true,
  matchedCount: 1,
  modifiedCount: 1
}

updateMany() - Update All Matching Documents

`updateMany()` updates all documents that match the filter.
<!-- Add a "prime" field to all electronics products -->
db.products.updateMany(
  { category: "electronics" },
  { $set: { prime: true } }
)

<!-- Increase all prices by 10% -->
db.products.updateMany(
  {},
  { $mul: { price: 1.1 } }
)

Using $inc to Increment
<!-- Increase view count by 1 -->
db.posts.updateOne(
  { title: "First Post" },
  { $inc: { views: 1 } }
)

<!-- Decrease stock by 5 (use negative value) -->
db.products.updateOne(
  { name: "Laptop" },
  { $inc: { stock: -5 } }
)

Working with Arrays

$push - Add to Array
<!-- Add a new tag to a post -->
db.posts.updateOne(
  { title: "First Post" },
  { $push: { tags: "javascript" } }
)

$pull - Remove from Array
<!-- Remove a tag from a post -->
db.posts.updateOne(
  { title: "First Post" },
  { $pull: { tags: "mongodb" } }
)

replaceOne() - Replace Entire Document

Unlike `updateOne()` which modifies specific fields, `replaceOne()` replaces the entire document with a new one.
db.users.replaceOne(
  { name: "John" },
  { name: "John", email: "john.new@example.com", phone: "555-1234" }
)

Upsert - Update or Insert

Sometimes you want to update a document if it exists, or insert it if it doesn't. Use the `upsert` option.
<!-- If user "Jane" exists, update her age; if not, create her -->
db.users.updateOne(
  { name: "Jane" },
  { $set: { age: 28, email: "jane@example.com" } },
  { upsert: true }
)

Practical Example: Blog Post Analytics
<!-- Insert a blog post -->
db.posts.insertOne({
  title: "MongoDB Updates",
  content: "Learning about update operations...",
  views: 0,
  likes: 0,
  tags: ["mongodb", "database"]
})

<!-- Someone viewed the post -->
db.posts.updateOne(
  { title: "MongoDB Updates" },
  { $inc: { views: 1 } }
)

<!-- Someone liked the post -->
db.posts.updateOne(
  { title: "MongoDB Updates" },
  { $inc: { likes: 1 } }
)

<!-- Add a new tag -->
db.posts.updateOne(
  { title: "MongoDB Updates" },
  { $push: { tags: "nodejs" } }
)

<!-- Check the final document -->
db.posts.findOne({ title: "MongoDB Updates" })

Two Minute Drill

  • `updateOne()` modifies the first matching document, `updateMany()` modifies all matches.
  • Use `$set` to change field values, `$inc` to increment numbers.
  • Array operators: `$push` adds, `$pull` removes elements.
  • `replaceOne()` replaces the entire document.
  • Upsert (`{ upsert: true }`) creates a document if it doesn't exist.
  • Always check `matchedCount` and `modifiedCount` to confirm your update worked.

Need more clarification?

Drop us an email at career@quipoinfotech.com