MongoDB Update Documentation

Source: Internet
Author: User
Tags documentation modifier mongodb update

Description: Look at the UPDATE statement for the relational database

UPDATE table name SET column name = new value WHERE Column name = a

Where the WHERE clause is similar to the query text, locates the child table to be changed, and the SET clause is similar to the modifier, which updates the contents.

1.1 Document replacement

is to completely replace the matching document with a new document. Used for migration of large-scale schema data.

For example:

If you want to migrate the friends and enemies two fields to the relationships subdocument. You can do the following

Step1:

var joe=db.users.findone ({"Name": "Joe"})

Joe.relationships={friend:joe.friends,enemies:joe.enemies}

The same principle gets:

Joe.username=joe.name;

Then delete the friends,enemies,name in the original document

Step2: Updating documents

Db.users.update ({name: "Joe"},joe)

Explain: {name: "Joe"} is the target document to be updated, and Joe is the modifier that explains what to modify

The last Joe document content:

1.2 Using modifiers

Usually the document will only have a subset of the data to be updated, you can use the atomic update modifier (updated modifier) to specify that some fields in the document should be modified.

Suppose: To store analytic data for a Web site in a collection, the counter is incremented whenever someone accesses the page. You can use the updater to modify the atomicity to accomplish this increase.

Each time someone accesses it, it finds the page through a URL and adds the value of "pageviews" with the "$inc" modifier

>db.analytics.update ({url: "www.example.com"},{"$inc": {pageviews:1}})

Then execute once Db.analytics.find ()

The data are as follows:

The data is increased by 1. To achieve the site statistics traffic requirements.

MongoDB Update Documentation

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.