Update Operations in MongoDB
MongoDB allows you to update existing documents in a collection using methods like updateOne()
and updateMany()
. These updates can modify specific fields, add new ones, increment values, or remove fields — all without replacing the entire document.
Step-by-Step: How Update Works
- Select the document(s) to update using a query filter
- Specify what to change using update operators like
$set
,$inc
,$unset
, etc. - Use
updateOne()
to update a single matching document orupdateMany()
to update all matching documents
Example 1: Using $set to Update a Field
Let’s first insert a sample document:
db.users.insertOne({
name: "Alice",
age: 25,
city: "Delhi"
});
Now we want to update Alice's city to "Mumbai":
db.users.updateOne(
{ name: "Alice" },
{ $set: { city: "Mumbai" } }
);
Output:
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
Explanation: The $set
operator updates the city
field to "Mumbai". If the field does not exist, it will be added.
Example 2: Updating Multiple Documents with updateMany()
Let’s insert more users:
db.users.insertMany([
{ name: "Bob", age: 30, city: "Delhi" },
{ name: "Charlie", age: 28, city: "Delhi" }
]);
Now update everyone living in "Delhi" to "Bangalore":
db.users.updateMany(
{ city: "Delhi" },
{ $set: { city: "Bangalore" } }
);
Output:
{ acknowledged: true, matchedCount: 2, modifiedCount: 2 }
Explanation: This updates all users where city
is "Delhi" to now say "Bangalore".
Example 3: Using $inc to Increment a Numeric Field
Suppose you want to increase Bob's age by 1:
db.users.updateOne(
{ name: "Bob" },
{ $inc: { age: 1 } }
);
Output:
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
Explanation: The $inc
operator adds the given value to the existing value of age
.
Example 4: Using $unset to Remove a Field
Let’s remove the city
field from Charlie’s document:
db.users.updateOne(
{ name: "Charlie" },
{ $unset: { city: "" } }
);
Output:
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
Explanation: The $unset
operator deletes the specified field from the document. The value you provide doesn’t matter (commonly an empty string).
Intuition Check
Q: What happens if you use updateOne()
without any operator like $set
?
A: It will replace the entire document with the new one, which can accidentally delete other fields. Always use update operators when modifying specific fields.
Bonus: Replace Entire Document
To replace the whole document, you can do this:
db.users.replaceOne(
{ name: "Alice" },
{ name: "Alice", age: 26 }
);
Output:
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
Note: The previous city
field will be removed since it's not included in the new object.
Summary
updateOne()
– updates the first document that matches the filterupdateMany()
– updates all matching documents$set
– modifies or adds a field$inc
– increments or decrements a field value$unset
– removes a field
Understanding and using MongoDB's update operators is key to managing data efficiently. They allow partial updates without rewriting entire documents, saving performance and avoiding errors.
Next Steps
In the next topic, we’ll explore how to delete documents from a collection using deleteOne()
and deleteMany()
.