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