Welcome back.
In this video, let's see how we can update record Inside Mongoose.
So let's go to the documentation and let's find the method called find by ID and update.
You can see we have one method call find by one and update.
So for this one we can use to find any document with any field, it can be named Title H or ID and we
have one called find by ID and update is also work in the same way.
So let's work with the find one and update and see how it's going to work.
So let's click on find one and update or we can set it here as find one and update.
So here we go.
And it will see that for this one it is in couple of arguments.
The first one going to be the query we want to find.
It can be a title, it can be ID and then the doc and the additional option.
I'll start.
So let's see how we are going to implement it inside that.
So it provides additional option as new.
It simply means that we want to see the updated data on the fly as soon as is being saved without refreshing.
So this option is really crucial or important and we see that default false and we change in version
point oh.
So I think with the new version, there is no need to provide the new because it should be updated and
we are using version 6.5.3.
So I think there's no need to provide a new argument.
So let's see how you're going to achieve that.
So back here, let me comment this one and let me bring this comment here and say update record.
So let's see how we are going to update this one.
So we bring in the student model on that.
We are going to use find one and update as soon as I type in fine.
I see all the method that I can use on that model and the one that I want is find one and update which
is this one and next is you pass in the options or the configuration.
The first one going to be the query.
So let's find the student by name of John as that and a nest egg means pass in is a field that you want
to update.
So I want to update the age to 35.
The NEST agreement going to be option like we saw in the documentation and then the first one going
to be new to true meaning that we want to see the updated one on the fly as soon as we save it.
So let's handle the premise then and then we have the student as the student, or you can make it as
updated student and let me catch any error if there is going to be an error here also.
All right, then.
Let me move this one and let's see.
So back to the terminal here.
Let me clear the terminal and let me run the file again instead of 20 of the age.
Now we have 30, meaning that it has been updated.
Let's try to remove the new and let's see if you're going to see the updated one based on what we've
learned.
Okay.
So now let me change the age to 40 years.
Here we go.
Back to the terminal here.
Let me clear it and let me run it again.
I could see that you couldn't get the updated one, but inside our DB, if you refresh it, we are going
to see that it is 40.
And you can see that we have it here, but we didn't see it as a response.
That's why the new equipment is important.
So let me bring back the new here and let me change this one to, let's say, 50 years.
We also have another method for updating.
So let me copy this one and comment it and the difference here.
Can I be fine by ID and update?
And this one finds only the ID, so find by I.D. and update.
So here we pass in the ID as a student.
So let's find the ID back to my campus here.
I think I want to find a manual here so I'll copy the ID and back to my code.
Here, let me replace it with that and then come on, separate it.
Now back to the terminal.
Let me run it again and let's see if I'm going to get Emanuel to be updated.
And you can see that Emanuel Fair has been updated.
All right.
So these are the common methods you are going to use a lot find by ID and update more than the fine
one and update.