MongoDB Learning Notes < three >

Source: Internet
Author: User

Continue to learn MongoDB, the main content of this study is to use the modifier to complete the local update operation

1. $set

Used to specify a key-value pair, which is modified if there is a key, and is added if it does not exist

Db.person.update ({"Name": "Xiaoming"},{$set: {"Age": 27}}) If the age key is present, the value of the age key is changed to 27, and if it does not exist, the age key is added to the document with a value of 27

2. $inc

Can only be used with numeric types, which can add the numeric value of a specified key to a specified size.

Db.person.update ({"Age": 27},{$inc: {"1}}") After this operation, the 1 is added

3. $unset

Deletes the specified key

Db.person.update ({"Age": 27},{$unset: {' Age ': 1}})

4. $push

-If the specified key is an array, the new value is added.

-If the specified key is not an array, the current operation is interrupted.

-Creates a key value pair for an array type if the specified key does not exist

Db.person.update ({"Name": "Xiaoming"},{$push: {books: "中文版"})

If books this array exists, it will be added to the new value 中文版

If books does not exist, the books array is created and the value is added to Chinese

If the books key already exists and is not an array, an error will be

5. $pushAll

You can add data to the array in batches

Db.person.uipdate ({"Name": "Xiaoming"},{$pushAll: ["Math", "Chinese", "PE"]})

6.addToSet

If the target array exists, it is not operational and does not exist, then add this entry

Db.person.update ({"Name": "Xiaoming"},{$addToSet: {books: "Math"}})

7. $pop

Removes a value from the specified array, 1 deletes the last value, 1 deletes the first value

Db.person.update ({"Name": "Xiaoming"},{$pop: {books:-1}})

8. $pull

Deletes a specified value

Db.person.update ({"Name": "Xiaoming"},{$pull: {books: "中文版"}})

9. $pullAll

Delete multiple specified values at once

Db.person.update ({"Name": "Xiaoming"},{$pullAll: {books:["中文版", "Math"]}})

10.$

Array locator, if the array has multiple values, we only want to operate on one part of it, we need to use the locator $

For example:

If there is a document {"name": "Xiaoming", books:[{"type": "JS", "name": "EXTJS4"},{"type": "JS", "name": "JQUERY"}},{"type": "DB", "name ":" MongoDB "}}]}

Want to add a document of type equal to JS to the same author author is Xiaohua

Db.person.update ({"Books.type": "JS"},{$set: {"Books.$.author": "Xiaohua"}})


Remember: The modifier is placed on the outermost, the following reference to the query is placed in the inner layer!

















MongoDB Learning Notes < three >

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.