MongoDB 2 ways to modify data _mongodb

Source: Internet
Author: User
Tags mongodb

In MongoDB, the Db.collection.ipdate () and Db.collection.save () methods can modify documents that already exist in the collection. The Db.collection.update () method provides additional control over the modification. For example, Db.collectoin.update () modifies a document that already has data or a set of matching query criteria. The Db.collection.save () method replaces an existing document with the same _id.

To modify multiple documents using the update () method:

By default, the update () method updates a document that satisfies the criteria. You can modify multiple documents by setting the multi option to true when calling a method. The following example modifies the Qty field for all Documents of Type field value "book"-1. The example uses $inc, which is a modified operator variable.

Copy Code code as follows:

Db.inventory.update (
{type: ' book '},
{$inc: {qty:-1}},
{Multi:true}
)

Use the Save () method to modify a document:

The Save () method replaces an existing document. Replaces a document with the Save () method by matching an existing document with the _id field. The following example completely replaces a document with _id 10 in the inventory collection:

Copy Code code as follows:

Db.inventory.save (
{
_id:10,
Type: "Misc",
Item: "Placard"
}
)

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.