Mongodb about update and findandmodify

Source: Internet
Author: User

db.collection.update :

Modifies an existing document or document in the collection. The method can modify the specific fields or documents of an existing document based on the update parameters, or completely replace the existing document.

By default, the update () method updates a single document. Set multiple parameters to update all documents that match the query criteria.

Updates can update multiple documents, but MONGODB only guarantees that the writing of a single document is atomic.

db.collection.findAndModify (document)

Modifies and returns a single document.

When you modify a single document, both the Findandmodify () and the Update () Methods atomically Update the document.

When you update a document, the Findandmodify () and update () methods operate differently:

By default, two actions modify an individual document. However, the update () method with multiple options can modify multiple documents.

If multiple documents match the update criteria, for findandmodify (), you can specify a sort to provide some control over the document to be updated.

Using the default behavior of the update () method, you cannot specify a single document to update when multiple documents match.

By default, Findandmodify () returns a pre-modified version of the document. To obtain an updated document, use the new option.

The update () method returns a Writeresult object that contains the state of the operation. To return to the updated document, use the Find () method. However, other updates may have modified the document between the update and the document retrieval. In addition, if the update modifies only a single document, but multiple documents match, you need to use additional logic to identify the updated document.

Mongodb about update and findandmodify

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.