MongoDB uses the update () and Save () methods to update the documents in the collection.
Update () method
The update () method is used to update a document that already exists. The syntax format is as follows:
db.. Update ( <query> <update>, {< Span class= "PLN" > Upsert: <boolean> , Multi: <boolean> , Writeconcern: < Document> })
Parameter description:
- Query: The search condition for update, similar to where in SQL update query.
- Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
- upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
- multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
- writeconcern : Optional, throws an exception level.
Instance
We insert the following data in the set col:
>Db.Col.Insert({Title: ' MongoDB Tutorial ', Description: MongoDB is a Nosql database ' , by: Rookie tutorial ' , Tags: [ ' MongoDB ' , ' database ' , "NoSQL" ], Likes:< Span class= "PLN" > 100})
We then updated the title with the update () method:
>Db.Col.Update({' Title ':' MongoDB Tutorial '},{$set:{' Title ':' MongoDB '}})Writeresult({ "Nmatched" : 1, "Nupserted" : 0, "Nmodified" : 1 }) # Output Information>Db.Col.Find().Pretty(){ "_ID" : ObjectId("56064f89ade2f21f36b03136"), "Title" : "MongoDB", "description" : "MongoDB is a Nosql database", "By" : "rookie Tutorial" , "url" : "http://www.runoob.com" , span class= "str" > "tags" : [ "MongoDB" , "database" , "NoSQL" ], "Likes" : 100}>
You can see the title (title) updated by the original "MongoDB tutorial" in order to "MongoDB".
The above statement only modifies the first found document, and if you want to modify multiple identical documents, you need to set the multi parameter to True.
>db. Col. Update({' title ':' mongodb tutorial '},{$set: {' title ':' MongoDB '}},{ Multi:true})
Save () method
The Save () method replaces an existing document with an incoming document. The syntax format is as follows:
DB. Collection. Save(<document>,{ writeconcern:<document>})
Parameter description:
- Document: documentation data.
- writeconcern : Optional, throws an exception level.
Instance
In the following example we have replaced the document data _id to 56064f89ade2f21f36b03136:
>Db.Col.Save({"_ID" : ObjectId("56064f89ade2f21f36b03136"), "Title" : "MongoDB", "description" : "MongoDB is a Nosql database", "by" : "Runoob" , "url" : "http://www.runoob.com" , span class= "str" > "tags" : [ "MongoDB" , "NoSQL" Span class= "pun" >], "likes" : 110})
After the substitution succeeds, we can view the replaced data through the Find () command
>Db.Col.Find().Pretty(){ "_ID" : ObjectId("56064f89ade2f21f36b03136"), "Title" : "MongoDB", "description" : "MongoDB is a Nosql database", "By" : "Runoob", "url" : "http://www.runoob.com", "tags" : [ "MongoDB", "NoSQL" ], "likes" : c30>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 article:
Db.col.update ({"Count": {$gt: 4}}, {$set: {"Test5": "OK"}},true,false);
Add in all:
Db.col.update ({"Count": {$gt: 5}}, {$set: {"Test5": "OK"}},true,true);
Update all:
Db.col.update ({"Count": {$gt: $}}, {$inc: {"Count": 1}},false,true);
Update only the first record:
Db.col.update ({"Count": {$gt: Ten}}, {$inc: {"Count": 1}},false,false);
UPDATE statement for MongoDB