Mongodb entry-11 update 1-mysql tutorial

Source: Internet
Author: User
Mongodb0000-11update 1mongodb0000-10delete the addition, deletion, and query of www.2cto.comdatabase201305213134.html mongodb have been completed. The rest is updated, and there are many updates. however, because of the large quantity of data, if we have learned it well, it will be much easier for us to use it in the future. update and use u in mongodb

Mongodb entry-11 Update 1 mongodb entry-10 delete http://www.2cto.com/database/201305/213134.html mongodb addition and deletion query has been completed, the rest is updated, the update content is more. however, because of the large quantity of data, if we have learned it well, it will be much easier for us to use it in the future. update and use u in mongodb

Mongodb entry-11 Update 1

Mongodb entry-10 delete

Http://www.2cto.com/database/201305/213134.html

The addition, deletion, and query of mongodb have been completed, and the rest is updated. There are many updates. however, because of the large quantity of data, if we have learned it well, it will be much easier for us to use it in the future.

Update in mongodb in the following format:

[Html]

Db. collection. update (criteria, objNew, upsert, multi)

Parameter description:

Criteria: sets query conditions to query which documents need to be updated.

ObjNew: updated object

Upsert: if the record already exists when it is set to true, update it. Otherwise, the default value of "false" is added.

Multi: when it is set to true, all documents that meet the query conditions will be updated. by default, only the first compliant document will be updated in mongodb.

Take a look at the following example code:

[Html]

> Db. user. find ()

{"_ Id": 1, "name": "user1", "age": 1}

{"_ Id": 2, "name": "user2", "age": 2}

{"_ Id": 3, "name": "user3", "age": 3}

{"_ Id": 4, "name": "user4", "age": 4}

{"_ Id": 5, "name": "user5", "age": 5}

{"_ Id": 6, "name": "user6", "age": 6}

> Db. user. update ({name: "user1"}, {name: "user11"}) --> we can omit the last two parameters.

> Db. user. find ()

{"_ Id": 1, "name": "user11 "}

{"_ Id": 2, "name": "user2", "age": 2}

{"_ Id": 3, "name": "user3", "age": 3}

{"_ Id": 4, "name": "user4", "age": 4}

{"_ Id": 5, "name": "user5", "age": 5}

{"_ Id": 6, "name": "user6", "age": 6}

We wanted to change user1 to user11, but the rest remained unchanged. However, we checked the query results and mongodb updated our entire document. therefore, if we use the method above to update the document, the entire document will be updated. note this. so how can we update user1 to user11 only? We only need to use the $ set Method of mongodb. I will explain it later.

[Html]

> Db. user. find ()

{"_ Id": 2, "name": "user2", "age": 2}

{"_ Id": 3, "name": "user3", "age": 3}

{"_ Id": 4, "name": "user4", "age": 4}

{"_ Id": 5, "name": "user5", "age": 5}

{"_ Id": 6, "name": "user6", "age": 6}

{"_ Id": 1, "name": "user1", "age": 1}

{"_ Id": 7, "name": "user1", "age": 7}

{"_ Id": 8, "name": "user1", "age": 8}

> Db. user. update ({name: "user1"}, {name: "user11"},) --> only one data record is updated, however, if we change the last parameter to 1 here, an error is returned because this parameter needs to be used together with the $ method.

> Db. user. find ()

{"_ Id": 2, "name": "user2", "age": 2}

{"_ Id": 3, "name": "user3", "age": 3}

{"_ Id": 4, "name": "user4", "age": 4}

{"_ Id": 5, "name": "user5", "age": 5}

{"_ Id": 6, "name": "user6", "age": 6}

{"_ Id": 1, "name": "user11 "}

{"_ Id": 7, "name": "user1", "age": 7}

{"_ Id": 8, "name": "user1", "age": 8}> db. user. update ({name: "user111" },{ name: "user111" },1,0) --> In this case, we update the document whose name is user111, but no, our third parameter is set to true, so a new document will be inserted at this time, and the result will be the same as below

> Db. user. find ()

{"_ Id": 2, "name": "user2", "age": 2}

{"_ Id": 3, "name": "user3", "age": 3}

{"_ Id": 4, "name": "user4", "age": 4}

{"_ Id": 5, "name": "user5", "age": 5}

{"_ Id": 6, "name": "user6", "age": 6}

{"_ Id": 1, "name": "user11 "}

{"_ Id": 7, "name": "user1", "age": 7}

{"_ Id": 8, "name": "user1", "age": 8}

{"_ Id": ObjectId ("519a2c8259827a002f7e5ace"), "name": "user111 "}

The fourth parameter is demonstrated below:

[Html]

> Db. user. update ({name: "user1" },{$ set: {name: "userNew)

> Db. user. find ()

{"_ Id": 2, "name": "user2", "age": 2}

{"_ Id": 3, "name": "user3", "age": 3}

{"_ Id": 4, "name": "user4", "age": 4}

{"_ Id": 5, "name": "user5", "age": 5}

{"_ Id": 6, "name": "user6", "age": 6}

{"_ Id": 1, "name": "user11 "}

{"_ Id": 7, "age": 7, "name": "userNew "}

{"_ Id": 8, "age": 8, "name": "userNew "}

{"_ Id": ObjectId ("519a2c8259827a002f7e5ace"), "name": "user111 "}

Here we use $ set, which only modifies some values in the document, rather than updating the entire document.

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.