The pit of the MongoDB data update

Source: Internet
Author: User

Statistics MongoDB slow query, found that some set slow query a lot, and then notify the development of the field to look at the index,

And after the development of the discussion with a unique index, add the time to find duplicate data, and then use the aggregate command to count 24w of data have 10w+ duplicate data,

Development said update operation when added {upsert:true}, should be the query not to add a new, no duplicate data,

Then look at the official documentation for MongoDB to view Db.collection.update, which has the following explanations

Use Unique Indexes

WARNING

To avoid inserting the same document more than once, only use Upsert: True if the query field is uniquely indexed.

Given A collection named people where no documents has a name field that holds the value Andy. Consider when multiple clients issue the following update with Upsert: true at the same time:

" Andy " " Andy " 1 1 true

If all Update () operations complete the query portion before any client successfully inserts data, Andthere is no unique I Ndex on the Name field and then each update operation may result in an insert.

To prevent MongoDB from inserting the same document more than once, create a unique index on the Namefield. With a unique index, if multiple applications issue the same update with Upsert: true, exactlyone update ( ) would successfully insert a new document.

The remaining operations would either:

    • Update the newly inserted document, or

    • Fail when they attempted to insert a duplicate.

      If the operation fails because of a duplicate index key error, applications may retry the operation which would succeed as An update operation.

The meaning is generally said: At the same time high concurrency upsert words, query operation completed, but not insert, at the same time insert more than the same data, in order to avoid this problem can increase the unique index, that is, the uniqueness of the data can only be guaranteed by a unique index,

At the same time, an example of a unique index and then high concurrency Update operation:

    1. Update the new data that has been inserted
    2. Because the unique index insert fails, it is best to add a retry mechanism so that the update succeeds.

The pit of the MongoDB data update

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.