⬅ Previous Topic
Query Filtering with OperatorsNext Topic ⮕
Delete Operations in MongoDB⬅ Previous Topic
Query Filtering with OperatorsNext Topic ⮕
Delete Operations in MongoDBMongoDB 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.
$set
, $inc
, $unset
, etc.updateOne()
to update a single matching document or updateMany()
to update all matching documentsLet’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" } }
);
{ 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.
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" } }
);
{ acknowledged: true, matchedCount: 2, modifiedCount: 2 }
Explanation: This updates all users where city
is "Delhi" to now say "Bangalore".
Suppose you want to increase Bob's age by 1:
db.users.updateOne(
{ name: "Bob" },
{ $inc: { age: 1 } }
);
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
Explanation: The $inc
operator adds the given value to the existing value of age
.
Let’s remove the city
field from Charlie’s document:
db.users.updateOne(
{ name: "Charlie" },
{ $unset: { city: "" } }
);
{ 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).
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.
To replace the whole document, you can do this:
db.users.replaceOne(
{ name: "Alice" },
{ name: "Alice", age: 26 }
);
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
Note: The previous city
field will be removed since it's not included in the new object.
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 fieldUnderstanding 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.
In the next topic, we’ll explore how to delete documents from a collection using deleteOne()
and deleteMany()
.
⬅ Previous Topic
Query Filtering with OperatorsNext Topic ⮕
Delete Operations in MongoDBYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.