MongoDB Learning Note 8 Deep MongoDB update action: modifier $set

Source: Internet
Author: User
Tags findone modifier mongodb update

MongoDB Chinese file in the database after the Update method updates the document, the Update method has two parameters, such as

Update (ARGS1,ARGS2)

ARGS1 refers to the condition of querying a document;

ARGS2 refers to what changes are made to the document being queried;

One, document replacement

> joe1= Db.post.findOne ({"Age": +}) {"_id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "name  ":" Joe "," Age ": 20}> joe1.age=2121> db.post.update ({" id ": 1},joe1) Writeresult ({" nmatched ": 1," nupserted " : 0, "nmodified": 1}) >

The modified result:

> joe1= Db.post.findOne ({"Age": +}) {"_id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "name ":" Joe "," Age ": 21}>

Second, the use of modified device

1. $set modifier

$set used to specify the value of a field that is created if the field does not exist;

> Db.post.findOne () {"_id": ObjectId ("54a530c3ff0df3732bac167f"), "id": 0, "name" : "Joe", "Age": 65}

To increase the comments key:

> db.post.update ({"id": 0}, ...    {$set: {"Comments": "I Love You"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.post.findOne () {"_id": Object ID ("54a530c3ff0df3732bac167f"), "id": 0, "name": "Joe", "Age": all, "comment S ":" I Love You "} >

To modify the value of the comments key:

> db.post.update ({"id": 0}, {$set: {"Comments": "I don ' t Love You"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "NM Odified ": 1}" > Db.post.findOne () {"_id": ObjectId ("54a530c3ff0df3732bac167f"), "id": 0, "name": "Joe", "Age": All, "comments": "I don t Love You"} >


Modify the value of the comments array:

> db.post.update ({"id": 0}, {$set: {"Comments": ["i love you", "or", "I don" T love  you "]})    writeresult ({ " nmatched " : 1, " nupserted " : 0,   "Nmodified"  : 1 })     > db.post.findone ({"id": 0})      {             "_id"  :  ObjectId ("54a530c3ff0df3732bac167f"),             " ID " : 0,            " name " : " Joe ",            " "Age"  : 65,              "Comments"  : [                      "I love  you ",                     " or ",                      "I don ' T love you"              ]    }    >


To remove the value of comments:

> db.post.update ({"id": 0}, {$unset: {"Comments": 1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})            > Db.post.findOne ({"id": 0}) {"_id": ObjectId ("54a530c3ff0df3732bac167f"), "id": 0, "Name": "Joe", "Age": $ >


2. Modify the inline document

> db.post.findone ({"id": 0})     {              "_id"  : objectid ("54a530c3ff0df3732bac167f"),              "id"  : 0,              "name"  :  "Joe",              "Age"  : 65,             " Comments " : {                      "1"  : 1,                      "2"  : 2,                      "3"  :  3            }    }    >  Db.post.update ({"id": 0}, {$set: {"Comments.1": 4})     writeresult ({  "nmatched"  : 1,  "nupserted"  : 0,  "nmodified"  : 1 })      > db.post.findone ({"id": 0})     {              "_id"  : objectid ("54a530c3ff0df3732bac167f"),              "id"  : 0,              "name"  :  "Joe",              "Age"  : 65,             " Comments " : {                      "1"  : 4,                      "2"  : 2,                      "3"  : 3             }    }    >


This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1598398

MongoDB Learning Note 8 Deep MongoDB update action: modifier $set

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.