MongoDB update documentation
MongoDB uses the update() and save() methods to update documents in a collection. Next, let us look at the applications of the two functions and their differences in detail.
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> } )
Parameter description:
##query : update query conditions, similar to sql update query behind where.
update : update object and some update operators (such as $, $inc...), etc., can also be understood as set within sql update query The following
upsert : Optional, this parameter means whether to insert objNew if there is no update record, true means insert, and the default is false. Not inserted.
#multi : Optional, mongodb default is false, only the first record found is updated. If this parameter is true, multiple conditions will be found. All records are updated.
writeConcern : Optional, the level at which the exception is thrown.
>db.col.insert({ title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'php中文网', url: 'http://www.php.cn', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })Then we update the title (title) through the update() method:
>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" : "php中文网", "url" : "http://www.php.cn", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } >You can see that the title has been updated from the original "MongoDB Tutorial" to "MongoDB". The above statement will only modify the first discovered document. If you want to modify multiple identical documents, you need to set the multi parameter to true.
>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})
save() method save() method replaces an existing document with the incoming document. The syntax format is as follows:
db.collection.save( <document>, { writeConcern: <document> } )
Parameter description:
- ##document
: document data.
- writeConcern
: Optional, the level at which the exception is thrown.
Example
In the following example, we replaced the document data with _id 56064f89ade2f21f36b03136:
>db.col.save({ "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php", "url" : "http://www.php.cn", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 })
After the replacement is successful, we can use find() Command to view the replaced data
>db.col.find().pretty() { "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "php", "url" : "http://www.php.cn", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 } >More examples
Only update the first record:
db.col.update( { " count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );Only update the first record: