Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB updates the documentation


May 17, 2021 MongoDB


Table of contents


MongoDB updates the data using the update() function


Describe

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

MongoDB data updates can use the update() function.

db.collection.update( criteria, objNew, upsert, multi )

The update() function accepts the following four parameters:

  • criteria: Update query conditions, similar to the sql update query inside where.
  • objNew : update objects and some updated operators (e.g. $, $inc... can also be understood as sql update query after the set
  • upsert : This parameter means that if there is no update record, whether to insert objNew, true for insertion, the default is false, do not insert.
  • Multi : mongodb defaults to false, updating only the first record found, and if this parameter is true, check out all the records as required.

In this tutorial we used the database name "myinfo" and the collection name "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 change the "password" field in the "userdetails" collection "user_id" to "QRSTBWN" to "NEWPASSWORD", then we can do so using the update() command (as shown in the following example).

If the criteria parameter matches any data in the collection, it executes a replacement command or a new data is inserted.

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

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

MongoDB updates the documentation


View the updated data in the collection

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

>db.userdetails.find();

MongoDB updates the documentation


More instances

Update only the first record:

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

All updates:

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

Add only the first one:

db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

Add it all in:

db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

All updates:

db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

Update only the first record:

db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );