MongoDB Learning Seven

Source: Internet
Author: User
Tags documentation mongodb tutorial mongodb update

MongoDB Update Documentation

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.collection.update (   <query>,   <update>,   {     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 ',    URL: ' http ://www.runoob.com ',    Tags: [' mongodb ', ' database ', ' NoSQL '],    likes: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 Info > Db.col.find (). Pretty () {        " _id ": ObjectId (" 56064f89ade2f21f36b03136 "),        " title ":" MongoDB ","        description ":" MongoDB is a Nosql database ",        " by ":" Rookie Tutorial ",        " url ":" Http://www.runoob.com ", 
    "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",    "tags": [            "MongoDB",            " NoSQL "    ],    " 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 ": [                " Mon Godb ",                " NoSQL "        ],        
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 Learning Seven

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.