Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb updates update

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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"]}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report