Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb updates update

Shulou Source: shulou.com Published: 2022-06-01 09:13:43 10月03日 Update

1.$inc

Usage: {$inc: {field:value}}

Function: add value to a field in a numeric field

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

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "name": "wang", "age": 20, "sex": "M"}

2.$set

Usage: {$set: {field:value}}

Function: set the value of a field field in the document to value

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

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "name": "wang", "age": 20, "sex": "F"}

3.$unset

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

Function: delete a field field

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

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "age": 20, "name": "wang"}

4.$push

Usage: {$push: {field:value}}

Function: append value to field. Note: field can only be an array type. If field does not exist, an array type will be inserted automatically.

> 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": 20, "name": "wang", "sex": ["F", "M"]}

5.pushAll

Usage: {$pushAll: {field:value_array}}

What it does: use the same as $push, except that $pushAll can append more than one value to an array field at a time.

> db.test1.update ({'name':'wang'}, {$pushAll: {' sex': ['M or Fundation recorder M and F']}})

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "age": 20, "name": "wang", "sex": ["F", "M", "M or F", "M and F"]}

> db.test1.update ({'name':'wang'}, {$push: {' sex': ['M or Fundation recorder M and F']}})

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "age": 20, "name": "wang", "sex": ["F", "M", "M or F", "M and F", ["M or F", "M and F"]]}

6.$addToSet

Usage: {$addToSet: {field:value}}

What it does: add a value to the array and increase it only if the value does not exist in the array.

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

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "age": 20, "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 in an array

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "age": 20, "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": 20, "name": "wang", "sex": ["F", "M", "M or F"]}

8.$pull

Usage: {$pull: {field:_value}}

Function: delete 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": 20, "name": "wang", "sex": ["M", "M or F"]}

10.$rename

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

Purpose: rename fields

> db.test1.update ({'name':'wang'}, {$rename: {' name':'names'}})

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

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

{"_ id": ObjectId ("58662477fb6a734e8f45133f"), "age": 20, "names": "wang", "sex": ["M", "M or F"]}

Tags: Function array field type just only multiple number document update Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Shulou Tech Info Xiaomi Linux