Summary of updates in MONGO

Source: Internet
Author: User

Updates in MONGO can also be used as additions

There are several ways to MONGO with the new

Save, UPDATE, Upsert

When you execute save, if the document has _id this parameter, save calls Upsert, otherwise it calls the insert

Upsert modified faster than update

Use Upsert if the modified data does not exist, a new data is created

Various modifiers

$inc, $set

Where $inc can only modify data of numeric types, modifying other types of data will cause an error

Db.ansheng.update ({"AAAAA": 20},{"$inc": {"AAAAA": -19}},true) Upsert
Db.ansheng.update ({"AAAAA": 20},{"$inc": {"AAAAA": -19}}) update
Db.ansheng.save ({"_id": ObjectId ("5706032acd0a6194868cf53e"), "AAAAA":) Save
When you use Save, be aware that the save changes are equivalent to replacing the document.
For example: The above save statement, when modifying the target data will only modify the AAAAA this field, the other fields will be erased
When using save, if the previous query parameter is not _id, he will create a new document

There are three simplest modifications, all of which are modified only by one field

When you use Save, it affects other fields.

Using update and Upsert will not affect other fields

Modification of arrays

{  "_id": ObjectId ("5706032acd0a6194868cf53e"),  "AAAAA":20.0,  " Book": ["looking for hope in despair","C # Daquan","MONGO authoritative Guide"]}

Modify the data in book

Db.ansheng.update ({"AAAAA": 20},{"$set": {"book.0": "Dry Your Sister 3,000"}},true)
Db.ansheng.update ({"AAAAA": 20},{"$set": {"book.0": "Dry Your Sister 3,000"})
Db.ansheng.save ({"_id": ObjectId ("5706032acd0a6194868cf53e"), "AAAAA": "Book": ["Looking for hope in despair", "C # Daquan", "MONGO authoritative guide"]} )

Summarize

Save, UPDATE, Upsert

Save can only be modified as a whole

Both update and Upsert can implement partial modifications

Update changes will not be modified if the same content is modified

Upsert will be modified regardless of the difference

Because the update changes the time will be first to query the database, if the same will not be modified, so the efficiency of update will be lower than upsert

So personal feeling

Upsert is the most efficient

The efficiency of the update is second

Save is the least efficient

Summary of updates in MONGO

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.