Common MongoDB operations-set 3 and mongodb-Set

Source: Internet
Author: User

Common MongoDB operations-set 3 and mongodb-Set

1. Update the document in the set. The syntax is as follows:

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

Parameter description:
Criteria: the object used to set query Conditions
ObjNew: the object used to set the update content
Upsert: if the record already exists, update it. Otherwise, a new record is added with a value of 0 or 1.
Multi: if there are multiple matching records, whether to update them all. The value is 0 or 1.

Note: by default, only the first qualified record is updated. Generally, the last two parameters are 0, 1, db. collection. update (criteria, objNew, 0, 1)

 

2. Update the document in the Set, and change the document name in the set to user1 as jack.

There are two problems with this modification: the new document will overwrite the original document. If the original document has multiple keys, after modification, it may be overwritten with only one key. The second problem is that even if there are multiple documents that meet the update conditions, only the first matching record will be updated.

 

3. Update the document in the set. use $ inc to add 1 to the age whose name is user1 in the set, and the other keys remain unchanged. $ inc indicates adding or subtracting a specified key value

 

4. Update the document in the set. $ set is used to specify a key value. If this key does not exist, create it.

For example:
To add an address to the document whose name is user1, run the following command:
Db. c1.update ({name: "user1"}, {$ set: {address: "bj)

Change the address of the document whose name is user1 to tj, and the other key-value pairs remain unchanged. The command is:
Db. c1.update ({name: "user1"}, {$ set: {address: "tj" }}, 0, 1)

 

5. Update the document in the set. $ unset is used to delete a key.

For example, to delete the address key in the document whose name is user1, run the following command: db. c1.update ({name: "user1"}, {$ unset: {address: 1)

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.