MongoDB usage update() And save() Method to update the documents in the collection. Next, let’s take a detailed look at the application of the two functions and their differences. 参数说明: query The query condition of update is similar to that after where in sql update query. update The object of update and some updated operators (such as $, $inc…) can also be understood as the following set in the sql update query upsert : optional, this parameter means that if there is no record of update, whether to insert objNew,true is insert, default is false, do not insert. multi : optional. Mongodb defaults to false. Only the first record found is updated. If this parameter is true, multiple records are checked out according to the condition and all of them are updated. writeConcern Optionally, the level at which the exception is thrown We’re gathering. Then we pass through You can see that the title (title) is updated from the original “MongoDB tutorial” to “MongoDB”. The above statement will only modify the first found document, if you want to modify multiple identical documents, you need to set the multi parameter to true. 参数说明: document Document data writeConcern Optionally, the level at which the exception is thrown In the following example, we replace the document data whose_ id is 56064f89ade2f21f36b03136: After the replacement is successful, we can pass the Update only the first record: Update all: Add only the first item: Add it all: Update all: Update only the first record: 3.14.1.
update()
Method ¶
update()
Method is used to update an existing document. The syntax format is as follows:db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
Example ¶
col
Insert the following data into:>db.col.insert({
title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '菜鸟教程',
url: 'http://www.runoob.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
update()
Method to update the title (title):>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) # 输出信息
> db.col.find().pretty()
{
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "菜鸟教程",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})
3.14.2.
save()
Method ¶
save()
The method replaces the existing document with the incoming document, and the_ id primary key is updated when it exists, and inserted if it does not exist. The syntax format is as follows:db.collection.save(
<document>,
{
writeConcern: <document>
}
)
Example ¶
>db.col.save({
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "Runoob",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"NoSQL"
],
"likes" : 110
})
find()
Command to view the replaced data>db.col.find().pretty()
{
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB 是一个 Nosql 数据库",
"by" : "Runoob",
"url" : "http://www.runoob.com",
"tags" : [
"mongodb",
"NoSQL"
],
"likes" : 110
}
>
3.14.3. More Instanc ¶
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} }
);
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"}
},false,true );
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"}
},true,false );
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"}
},true,true );
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1}
},false,true );
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1}
},false,false );