MongoDB Update Documentation

Source: Internet
Author: User
Tags mongodb tutorial mongodb update

MongoDB uses the update () and Save () methods to update the documents in the collection. Next, let's take a look at the application of the two functions and their differences.

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);

MongoDB Update Documentation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.