MongoDB Update Documents
In MongoDB, the operation of updating documents can be implemented using multiple methods. Commonly used methods include:updateOne(), updateMany(), replaceOne(), and findOneAndUpdate()。
1、updateOne()
The updateOne() method is used to update a single document that matches the filter.
Syntax:
db.collection.updateOne(filter, update, options)
- filter: The query condition used to find documents.
- update: Specifies the document or update operator for the update operation.
- options: Optional parameter object, such as
upsert、arrayFiltersetc.
Example
{ name: "Alice" }, // Filter condition
{ $set: { age: 26 } }, // Update operation
{ upsert: false } // Optional parameters
);
2、updateMany()
The updateMany() method is used to update all documents that match the filter.
Syntax:
db.collection.updateMany(filter, update, options)
- filter: The query condition used to find documents.
- update: Specifies the document or update operator for the update operation.
- options: Optional parameter object, such as
upsert、arrayFiltersetc.
Example
{ age: { $lt: 30 } }, // Filter condition
{ $set: { status: "active" } }, // Update operation
{ upsert: false } // Optional parameters
);
3、replaceOne()
The replaceOne() method is used to replace a single document that matches the filter. The new document will completely replace the old document.
Syntax:
db.collection.replaceOne(filter, replacement, options)
- filter: The query condition used to find documents.
- replacement: The new document that will replace the old document.
- options: Optional parameter object, such as
upsertetc.
Example
{ name: "Bob" }, // Filter condition
{ name: "Bob", age: 31 } // New document
);
4、findOneAndUpdate()
The findOneAndUpdate() method is used to find and update a single document, and can optionally return the document before or after the update.
Syntax:
db.collection.findOneAndUpdate(filter, update, options)
- filter: The query condition used to find documents.
- update: Specifies the document or update operator for the update operation.
- options: Optional parameter object, such as
projection、sort、upsert、returnDocumentetc.
Example
{ name: "Charlie" }, // Filter condition
{ $set: { age: 36 } }, // Update operation
{ returnDocument: "after" } // Optional parameters, return the updated document
);
Option Parameters
The options parameter of these update methods can usually include the following options:
- upsert: Whether to insert a new document if no matching document is found.
- arrayFilters: When updating nested arrays, specifies the condition for the array elements that should be updated.
- collation: Specifies the collation to use when comparing strings.
- returnDocument: Used in findOneAndUpdate, specifies whether to return the document before the update ("before") or after the update ("after").
Simple Example
Update a single document:
db.myCollection.updateOne(
{ name: "Alice" },
{ $set: { age: 26 } }
);Update multiple documents:
db.myCollection.updateMany(
{ age: { $lt: 30 } },
{ $set: { status: "active" } }
);Replace a single document:
db.myCollection.replaceOne(
{ name: "Bob" },
{ name: "Bob", age: 31 }
);
Find and update a single document:
db.myCollection.findOneAndUpdate(
{ name: "Charlie" },
{ $set: { age: 36 } },
{ returnDocument: "after" }
);
In the following example, we replaced the document data with _id 56064f89ade2f21f36b03136:
>db.col.save({
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "Example",
"url" : "http://www.example.com",
"tags" : [
"mongodb",
"NoSQL"
],
"likes" : 110
})
After the replacement is successful, we can use the find() command to view the replaced data.
>db.col.find().pretty()
{
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "Example",
"url" : "http://www.example.com",
"tags" : [
"mongodb",
"NoSQL"
],
"likes" : 110
}
>
More Examples
Update only the first record:
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
Update all:
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
Insert only the first record:
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
Insert all:
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
Update all:
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
Update only the first record:
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false ); Other Extensions