MongoDB Update Documentation

Source: Internet
Author: User
Tags mongodb update

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:
The query:update query condition, similar to that in SQL Update query, is behind.
Update:update objects 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 collection MyCol:

Db.mycol.insert ({"_id": +, "name": "Liang", "title": "MongoDB Overview"})
Db.mycol.insert ({"_id": 101, "name": "Guo", "title": "MongoDB Guide"})
Db.mycol.insert ({"_id": 102, "name": "June", "title": "NoSQL Database"})
Db.mycol.insert ({"_id": 104, "name": "Liuzhou", "title": "Python Quick Guide"})

Update the title with the update () method:
Db.mycol.update ({"Name": "Liang"},{$set: {"title": "MongoDB Overview2"}})

Db.mycol.find ();
{"_id": +, "name": "Liang", "title": "MongoDB Overview2"}
{"_id": 101, "name": "Guo", "title": "MongoDB Guide"}
{"_id": 102, "name": "June", "title": "NoSQL Database"}
{"_id": 104, "name": "Liuzhou", "title": "Python Quick Guide"}


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.mycol2.insert ({"_id": +, "name": "Liang", "title": "MongoDB Overview"})
Db.mycol2.insert ({"_id": 101, "name": "Liang", "title": "MongoDB Guide"})
Db.mycol2.insert ({"_id": 102, "name": "Liang", "title": "NoSQL Database"})
Db.mycol2.insert ({"_id": 104, "name": "Liang", "title": "Python Quick Guide"})

Db.mycol2.update ({' name ': ' Liang '},{$set: {' title ': ' MongoDB '}},{multi:true})

Db.mycol2.find ();
{"_id": +, "name": "Liang", "title": "MongoDB"}
{"_id": 101, "name": "Liang", "title": "MongoDB"}
{"_id": 102, "name": "Liang", "title": "MongoDB"}
{"_id": 104, "name": "Liang", "title": "MongoDB"}


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

Db.mycol3.insert ({"_id": +, "name": "Liang", "title": "MongoDB"})

Db.mycol3.save ({"_id": +, "name": "Liang2", "title": "MongoDB2"})


Db.mycol3.find ()
{"_id": +, "name": "Liang2", "title": "MongoDB2"}


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.