Mongodb entry-12 Update 2

Source: Internet
Author: User
Mongodb0000-12update 2mongodb0000-11update 1www.2cto.comdatabase201305213137.html continue to introduce mongoDB updates. $ inc adds a value to a field. This method can only operate on numbers, that is, only one value can be added to a number. Of course, this value can be a negative number. [html] db. user. find ()

Mongodb entry-12 Update 2 mongodb entry-11 update 1 http://www.2cto.com/database/201305/213137.html continue to introduce mongoDB updates. $ inc adds a value to a field. This method can only operate on numbers, that is, only one value can be added to a number. Of course, this value can be a negative number. [html] db. user. find ()

Mongodb entry-12 Update 2

Mongodb entry-11 update 1

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

Continue to introduce mongoDB updates.

$ Inc

Add a value to a field. This method can only operate on numbers. That is to say, only one value can be added to a number. Of course, this value can be negative.

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

> Db. user. update ({name: "user2" },{ $ inc: {age: 10 }}) --> Add 10 to the age of user2.

> Db. user. find ()

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

{"_ 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: "user6" },{$ inc: {age:-4 }}) --> Add-4 to the age of user6, that is, subtract 4

> Db. user. find ()

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

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

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

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

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

$ Set

When this field is included in this document, update this field. If this field is not included in this document, add a field for this document.

[Html]

> Db. user. find ()

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

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

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

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

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

> Db. user. update ({name: "user2" },{ $ set: {age: 20 }}) --> set age with name user2 to 20

> Db. user. find ()

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

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

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

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

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

> Db. user. update ({name: "user2" },{$ set: {sex: "nan" }}) --> set the sex with name user2 to nan, but this field does not exist, therefore, add the sex field to this document and assign it to nan.

> Db. user. find ()

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

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

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

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

{"_ Id": 2, "age": 20, "name": "user2", "sex": "nan "}

$ Unset

Delete a field in the document.

[Html]

> Db. user. find ()

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

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

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

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

{"_ Id": 2, "age": 20, "name": "user2", "sex": "nan "}

> Db. user. update ({name: "user2" },{$ unset: {sex: 1 }}) --> Delete the sex field whose name is user2. if you delete a field that does not exist, no error is reported.

> Db. user. find ()

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

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

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

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

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

$ Push

Store a number into an array in three cases. If this field exists, the number is directly saved to the array. if this field does not exist, create a field and insert the number into the array. if the updated field is not an array, an error is returned.

[Html]

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4]}

{"_ Id": 2, "text": "test "}

> Db. test. update ({_ id: 1}, {$ push: {ary: 5}) --> directly store the Array

> Db. test. find ()

{"_ Id": 2, "text": "test "}

{"_ Id": 1, "ary": [1, 2, 3, 4, 5]}

> Db. test. update ({_ id: 2 },{ $ push: {ary: 6 }}) --> Create an array and store it

> Db. test. find ()

{"_ Id": 2, "ary": [6], "text": "test "}

{"_ Id": 1, "ary": [1, 2, 3, 4, 5]}

> Db. test. update ({_ id: 2}, {$ push: {text: 6}) --> the update field exists but is not an Array Error.

Cannot apply $ push/$ pushAll modifier to non-array

If we want to press multiple values together, we may directly store an array, but this is not correct. $ push will only store one field at a time. The Code is as follows:

[Html]

> Db. test. update ({_ id: 1}, {$ push: {ary: [6, 7]})

> Db. test. find ()

{"_ Id": 2, "ary": [6], "text": "test "}

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7]}

To implement the above functions, we can use the following $ pushAll

$ PushAll

Store multiple values into an array at a time.

[Html]

> Db. test. update ({_ id: 1}, {$ pushAll: {ary: [8, 9]})

> Db. test. find ()

{"_ Id": 2, "ary": [6], "text": "test "}

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

$ AddToSet

Similar to the $ push function, a number is stored in an array. The difference is that if this number exists in the array, it will not be inserted, but will only Insert new data. There are also three situations, same as $ push.

[Html]

> Db. test. find ()

{"_ Id": 2, "ary": [6], "text": "test "}

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

> Db. test. update ({_ id: 2}, {$ addToSet: {ary: 7}) --> There Is No 7 in ary. The insertion is successful.

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

{"_ Id": 2, "ary": [6, 7], "text": "test "}

> Db. test. update ({_ id: 2}, {$ addToSet: {ary: 7}) --> if there is 7 in ary, insertion fails.

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

{"_ Id": 2, "ary": [6, 7], "text": "test "}

In the other two cases, perform the test on your own.

$ Pop

Delete the last element of the array

[Html]

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

{"_ Id": 2, "ary": [6, 7], "text": "test "}

> Db. test. update ({_ id: 2 },{ $ pop: {ary: 1 }})

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

$ Pull

Deletes an element in an array. If the Deleted field is not an array, an error is returned.

[Html]

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 8, 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

> Db. test. update ({_ id: 1}, {$ pull: {ary: 8 }})

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

$ PullAll

Delete multiple values in the array, which is similar to pushAll and push.

[Html]

> Db. test. find ()

{"_ Id": 1, "ary": [1, 2, 3, 4, 5, [6, 7], 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

> Db. test. update ({_ id: 1}, {$ pullAll: {ary: [, 8]})

> Db. test. find ()

{"_ Id": 1, "ary": [3, 4, 5, [6, 7], 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

$ Rename

Rename a field

[Html]

> Db. test. find ()

{"_ Id": 1, "ary": [3, 4, 5, [6, 7], 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

> Db. test. update ({_ id: 1}, {$ rename: {ary: "aryNew "}})

> Db. test. find ()

{"_ Id": 1, "aryNew": [3, 4, 5, [6, 7], 9]}

{"_ Id": 2, "ary": [6], "text": "test "}

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.