MongoDB Updates Update

Source: Internet
Author: User

1. $inc

Usage: {$inc: {Field:value}}

Function: Adds value to a field of a number

> db.test1.update ({' name ': ' Wang '},{$inc: {age:1}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "name": "Wang", "age": +, "sex": "M"}


2. $set

Usage: {$set: {Field:value}}

Function: Sets the value of a field in a document

> db.test1.update ({' name ': ' Wang '},{$set: {' sex ': ' F '}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "name": "Wang", "age": +, "sex": "F"}


3. $unset

Usage: {$unset: {field:1}}

Role: Delete a field

> db.test1.update ({' name ': ' Wang '},{$unset: {' Sex ': 1}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang"}


4. $push

Usage: {$push: {Field:value}}

Function: Append value to field. Note: field can only be an array type, and if field does not exist, an array type is automatically inserted

> db.test1.update ({' name ': ' Wang '},{$push: {' sex ': ' F '}})

> db.test1.update ({' name ': ' Wang '},{$push: {' sex ': ' M '}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": ["F", "M"]}


5.pushAll

Usage: {$pushAll: {Field:value_array}}

Function: Use the same as $push, except that $pushall can append multiple values to an array field at a time.

> db.test1.update ({' name ': ' Wang '},{$pushAll: {' sex ': [' m or f ', ' M and F '}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": ["F", "M", "M or F", "M and F"] }


> db.test1.update ({' name ': ' Wang '},{$push: {' sex ': [' m or f ', ' M and F '}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": ["F", "M", "M or F", "M and F", ["M or F", "M and F"]] }


6. $addToSet

Usage: {$addToSet: {Field:value}}

Function: Adds a value to the array, and only increases if the value does not exist in the array.

> db.test1.update ({' name ': ' Wang '},{$addToSet: {' sex ': [' M or F ', ' MF ']}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": [ "F", "M", "M or F", "M and F", [ "M or F", "M and F"], [ "M or F", "MF"]}


7. $pop

Usage: Delete the first value in the array: {$pop: {field:-1}}, delete the last value in the array: {$pop: {field:1}}

Function: Used to delete a value within an array

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": ["F", "M", "M or F", "M and F"] }

> db.test1.update ({' name ': ' Wang '},{$pop: {' Sex ': 1}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": ["F", "M", "M or F"]}


8. $pull

Usage: {$pull: {Field:_value}}

Function: Removes a value equal to _value from the array field

> db.test1.update ({' name ': ' Wang '},{$pull: {' sex ': ' F '}})

> db.test1.find ({' name ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "name": "Wang", "Sex": ["M", "M or F"]}


Ten. $rename

Usage: {$rename: {old_field_name:new_field_name}}

Role: Renaming a field

> db.test1.update ({' name ': ' Wang '},{$rename: {' name ': ' Names '}})

> db.test1.find ({' name ': ' Wang '})

> db.test1.find ({' names ': ' Wang '})

{"_id": ObjectId ("58662477fb6a734e8f45133f"), "age": +, "names": "Wang", "Sex": ["M", "M or F"]}


MongoDB Updates Update

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.