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)