w3resource

Update data in MongoDB with update() function

Description

In this page, we are going to discuss how to update data into a collection. The update can be done with update(). The update() takes four arguments - criteria, objectnew, upsert and multi.

criteria - Query which specify the record to update;

objectnew- Specify the updated information or it can be used by $ operator (i.e. $inc...).

upsert - An upsert do update the record if match the criteria and insert a record if not match.

multi - This argument asks for updates all matching rows or just the first one(which is default).

Our database name is 'myinfo' and our collection name is 'userdetails'. Here, inserting two more records.

> 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 re-write the document into the collection 'userdetails' with a change of 'password' is 'NEWPASSWORD' where 'user_id' is 'QRSTBWN' the following update() command can be written. If the criteria argument matches with any record the update will take place otherwise a new record will be inserted. The following example will update the first matching criteria into the collection.

> 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"]});

Command written in command prompt

View the updated data into the collection

>db.userdetails.find().pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

{
        "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"
        ]
}
{
        "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"
        ]
}

Document written in command prompt

Previous: MongoDB INSERT
Next: MongoDB DELETE



Follow us on Facebook and Twitter for latest update.