MongoDB Update Documents
In MongoDB, document update operations can be implemented in a variety of ways. 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 the document.
- update: specifies the document or update operator for the update operation.
- options: optional parameter object, such as
upsert、arrayFilters, etc.
Example
db.
myCollection
.
updateOne
(
{
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 the document.
- update: specifies the document or update operator for the update operation.
- options: optional parameter object, such as
upsert、arrayFilters, etc.
Example
db.
myCollection
.
updateMany
(
{
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 the document.
- replacement: the new document, which will replace the old document.
- options: optional parameter object, such as
upsert, etc.
Example
db.myCollection.replaceOne(
{ 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 the document.
- update: specifies the document or update operator for the update operation.
- options: optional parameter object, such as
projection、sort、upsert、returnDocument, etc.
Example
db.
myCollection
.
findOneAndUpdate
(
{
name
:
"Charlie"
}
,
// Filter condition
{
$set
:
{
age
:
36
}
}
,
// Update operation
{
returnDocument
:
"after"
}
// Optional parameter, return the updated document
)
;
Optional 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 to update.
- collation: specifies the collation used when comparing strings.
- returnDocument: used in findOneAndUpdate to specify returning the document before ("before") or after ("after") the update.
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 replace the document data whose _id is 56064f89ade2f21f36b03136:
>db.col.save({
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "Runoob",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"NoSQL"
],
"likes" : 110
})
After a successful replacement, we can view the replaced data with the find() command
>db.col.find().pretty()
{
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "Runoob",
"url" : "http://www.runoob.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 );
Add only the first one:
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
Add all of them:
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 );