MongoDB Update Documentation

Source: Internet
Author: User
Tags documentation mongodb mongodb update

MongoDB useUpdate () function updates data
Describe

In this section we will begin to learn how to update the collection data in MongoDB.

The update () function can be used for MONGODB data updates.

Db.collection.update (criteria, objnew, Upsert, Multi)

The update () function accepts the following four parameters:

    • criteria : The query condition for update, similar to where in SQL update query.
    • objnew : Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • Upsert : This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted.
    • Multi : MongoDB default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.

In this tutorial we use the database name "MyInfo", the collection name is "Userdetails", and the following is the inserted data:

> document= ({"user_id": "Mnopbwn", "Password": "Mnopbwn", "Date_of_join": "16/10/2010"
, "Education": "M.C.A.", "profession": "consultant", "interest": "Music", "Community_Name": ["Modern Music",
"Classical music", "WESTERN Music"], "community_moder_id": ["Mr BBB", "Mr Jjj", "Mr MMM"], "community_members":
[500,200,1500], "friends_id": ["MMM123", "NNN123", "OOO123"], "ban_friends_id": ["BAN123", "BAN456", "BAN789"]});
> Db.userdetails.insert (document)
> document= ({"user_id": "Qrstbwn", "Password": "Qrstbwn", "Date_of_join": "17/10/2010", "Education": "M.B.A."
, "profession": "MARKETING", "Interest": "Music", "Community_Name": ["Modern Music", "classical Music", "WESTERN
MUSIC "]," community_moder_id ": [" Mr BBB "," Mr Jjj "," Mr MMM "]," community_members ": [500,200,1500]," friends_id ":
["MMM123", "NNN123", "OOO123"], "ban_friends_id": ["BAN123", "BAN456", "BAN789"]});
> Db.userdetails.insert (document)
Update () command

If we want to modify the password field of "user_id" as "Qrstbwn" in the "Userdetails" collection to "NewPassword", then we can use the update () command (as shown in the following example).

If the criteria argument matches any of the data in the collection, it will execute the Replace command, or a new data will be inserted.

The following instance updates the data for the first matching criterion:

> db.userdetails.update ({"user_id": "Qrstbwn"},{"user_id": "Qrstbwn", "Password": "NewPassword"
, "Date_of_join": "17/10/2010", "Education": "M.B.A.", "profession": "MARKETING", "interest":
"Music", "Community_Name": ["Modern Music", "classical Music", "WESTERN Music"], "community_moder_id": ["MR.
BBB "," Mr Jjj "," Mr MMM "]," community_members ": [500,200,1500]," friends_id ": [" MMM123 "," NNN123 "," OOO123 "]," ban_ friends_id ": [" BAN123 "," BAN456 "," BAN789 "]});


To view the updated data in the collection

We can use the following command to see if the data is updated:

>db.userdetails.find ();


More examples

Update only the first record:

Db.test0.update ({"Count": {$gt: 1}}, {$set: {"test2": "OK"}});

Update all:

Db.test0.update ({"Count": {$gt: 3}}, {$set: {"test2": "OK"}},false,true);

Add only the first article:

Db.test0.update ({"Count": {$gt: 4}}, {$set: {"Test5": "OK"}},true,false);

Add in all:

Db.test0.update ({"Count": {$gt: 5}}, {$set: {"Test5": "OK"}},true,true);

Update all:

Db.test0.update ({"Count": {$gt: $}}, {$inc: {"Count": 1}},false,true);

Update only the first record:

Db.test0.update ({"Count": {$gt: Ten}}, {$inc: {"Count": 1}},false,false);

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.