MongoDB using update () and save () method to update the collection of documents:
update () method:
update () method is used to update the existing document. Syntax is as follows:
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
Parameter Description:
- Query : query update, similar to the back where within sql update query.
- Update : Update objects and some of the newer operators (e.g., $, $ inc is ...) and the like, may be understood as a set behind the sql update query
- the upsert : Alternatively, this parameter means that, if the record does not exist in the update, whether to insert objNew, true inserted, the default is false, not inserted (equivalent to insertion).
- Multi : Optional, mongodb default is false, only updating the first record found, if this parameter is true, according to the conditions put many records to check out all the updates.
- writeConcern : Optional, throw-level exception.
Examples
We insert the following data set in col:
> db.col.insert ({ title: 'Tutorial MongoDB', Description: 'MongoDB is a database Nosql', by: 'novice tutorial', URL: 'http://www.runoob.com', Tags: [ ' MongoDB ',' Database ',' the NoSQL '], Likes: 100 })
Then we have to update the title by the update () method (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 } >
We can see the title (title) from the original "MongoDB Tutorial" Update to "MongoDB."
The above statement will only modify a document found in the first, if you want to modify the same document and more, you need to set multi parameter to true.
>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})
save () method
save () method to replace the existing document by the incoming document. Syntax is as follows:
db.collection.save( <document>, { writeConcern: <document> } )
Parameter Description:
- the Document : document data.
- writeConcern : Optional, throw-level exception.
Examples
The following examples we replace the document data _id as 56064f89ade2f21f36b03136 of:
>db.col.save({ "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Runoob", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 })
After the replacement is successful, we can () command to view the data by replacing the find
>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
Only the first record update: db.col.update ({ "COUNT": {$ gt:. 1}}, {$ SET: { "test2": "the OK"}}); total update: db.col.update ({ "count": {$ gt: 3}}, {$ set: { "test2": "OK"}}, false, true); add only the first: db.col.update ({ "COUNT" : {$ gt: 4}} , {$ set: { "test5": "OK"}}, true, false); add all added to the list: db.col.update ({ "COUNT": {$ gt:. 5 }}, {$ set: { "test5": "OK"}}, true, true); total update: db.col.update ({ "COUNT": {$ gt: 15}}, {$ inc is: { "count": 1}}, false, true); update only the first record: db.col.update ({ "COUNT": {$ gt: 10}}, {$ inc is: { "COUNT":}. 1 }, false, false);