MongoDB | Document Updating in MongoDB
9K views
Oct 24, 2024
Document Updating in MongoDB
View Video Transcript
0:00
In this video we are discussing document updating in MongoDB
0:05
So, document is pre-existing. We are going to update its corresponding value
0:12
So to update documents, we will be using the method, that is the update method
0:18
And for the first match and update many for all the documents
0:23
So we are having two methods here. One is the update method for the first match and update many for all the documents
0:29
So, syntax will be like this, db. .collection name, dot update, then selection logic and updated data
0:40
So, DB. . . . . . . . . this is a method name, passing parameters, that is the selection logic
0:46
and updated data. So let us go for one practical demonstration for the better understanding of this concept
0:54
In this video, we are going to discuss that how we can perform the update operation on MongoDB
0:59
So this is the current content of our employee collection. So this is the current content we are having and this video is the continuation of the previous video also
1:09
So now let us go for some update queries So we shall be executing couple of update queries so that you can get the idea So the first update query has been given as that update the age of Vivek to 23
1:22
So you can find that this age of Vivek is actually 20 here. So I want to update this age to 23
1:30
So how to do that one? So we'll be going for db.mployee
1:36
Dot update. So the method is update. and then we shall go for there is a name colon Vivek the name colon
1:47
Vivek within the double quotes and then Calibrase complete so that is a one
1:53
JSON object and then we are passing this one as there is a dollar set
1:59
colon within Calibrase we are going for this age colon 23 So Calibres complete another time Calibre is complete another time first bracket complete
2:13
so this much you have done so let me go for this execution so right result matched
2:21
one so let us go for the display now so I going for this display so you can find that this age of Vivek has got updated to 23 Initially it was 20 Initially it was 20 now it has become 23 So update operation has been
2:37
carried out successfully. So let us go for another query here. So increase the
2:43
salary by 2% for all employees who live in Kolkata. So increase in the salary of all
2:50
employees who live in Kolkata. So how to do that one? So we shall be going for DB
2:55
employee dot update in this case we shall not give only update because I want to do
3:04
the changes for all occurrences of the city Kolkata so I shall go for update many
3:08
so the method name will be update many and this part is very simple we shall be
3:13
going for city colon then Kolkata city colon Kolkata and then the
3:20
calibus will get completed then comma then I shall go for dollar mal colon we shall go for salary colon 1.0 2 so we'll be doing the
3:38
multiplication so I think here I shall be putting this Calibus so Calibus complete then fastback it complete so DB dot update many so the here the method is update
3:52
many because I want to do these changes for all occurrences of Kolkata city
3:58
Kolkata is city so city colon Kolkata so now we'll be going for dollar
4:04
mal colon salary colon 1.0 so let me go for this execution so
4:10
here we are having a match count two and modified count is also true and acronologist is true
4:17
so match count two means because it is having in two places we are having this Kolkata so
4:22
this is a place number one and this is a place number two so here we are having this
4:26
salary is 16,000 and here we are having the salary of 18,000 so let me go for the find
4:32
now so you can find that this 16,000 has become 16,320 and this
4:40
18,000 has become 18,360 because in the both of these cases we're having this
4:47
Kolkata as a value for the city for the city key so in this way in this
4:52
demonstration we have shown you that how this update operation can be carried out
4:57
on our MongoDB thanks for watching this video
#Programming