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