Updating Documents in MongoDB
So you've inserted some data, but now you need to change it. Maybe a user changed their email, or you need to increment a counter. Updating in MongoDB is straightforward—you tell it what to change and what to change it to. No complex UPDATE statements with JOINs and subqueries.
You've got updateOne() and updateMany() as your main tools. updateOne() changes a single document, updateMany() changes all matching documents. Both require an update operator to specify what kind of change you're making. You can't just pass a new document directly—you need to use operators like $set or $inc.
Why operators? Because MongoDB needs to know exactly what you're doing. Are you replacing a field? Adding to an array? Incrementing a number? Each operation has its own operator, and using them correctly is the key to mastering MongoDB updates.
The $set Operator
The $set operator is your bread and butter for updates. It sets the value of a field to the specified value. If the field doesn't exist, it creates it. If it does exist, it overwrites the current value. Think of it like editing a specific line in a document without touching anything else.
This is the most common update operator you'll use. Whether you're updating a user's profile, changing a product's price, or modifying any field, $set is usually what you reach for. It's safe, predictable, and does exactly what you'd expect.
db.users.updateOne(
{ name: "Alice Johnson" },
{ $set: { email: "alice.new@example.com" } }
)
db.users.updateOne(
{ name: "Bob Smith" },
{ $set: { age: 36, "address.city": "Chicago" } }
)
Try it Yourself →
Other Update Operators
Beyond $set, MongoDB gives you a whole toolbox of update operators. $inc increments a number field by a specified value—perfect for counters, likes, or any numeric value that goes up or down. $push adds an element to an array, while $pull removes elements that match a condition.
These operators are what make MongoDB so flexible. Instead of fetching a document, modifying it in your application, and saving it back, you can let MongoDB handle the modification directly. It's faster, safer, and reduces the chance of race conditions.
db.users.updateOne(
{ name: "Alice Johnson" },
{ $inc: { age: 1 } }
)
db.users.updateOne(
{ name: "Bob Smith" },
{ $push: { hobbies: "gaming" } }
)
db.users.updateOne(
{ name: "Carol White" },
{ $pull: { hobbies: "reading" } }
)
Try it Yourself →
Updating Many Documents
Sometimes you need to update more than one document at a time. Maybe you want to add a "verified" flag to all users who confirmed their email, or you need to increment a view count across multiple posts. That's where updateMany() shines.
The same operators work with updateMany() as with updateOne()—the only difference is scope. updateMany() applies the update to every document that matches your filter. Use it wisely, because a poorly written filter can affect way more documents than you intended. Always test your filter with find() first.
db.users.updateMany(
{ age: { $lt: 30 } },
{ $set: { status: "young" } }
)
db.products.updateMany(
{ stock: { $lte: 10 } },
{ $set: { needsReorder: true } }
)
Try it Yourself →