Now that we've seen the basics of the promised syntax we're gonna go ahead and explore how we can update
documents using Mongo D.B..
And we're gonna do so with promises instead of callbacks.
So to start I'm going to close both of the open files from the playground directory and I'm going to
bring my tree view back into view from here.
The only file we need access to is the one we've been using mostly in this section.
Mongo D.B. dot J S now down below.
We do want to switch the terminal directory as well.
I'll use CDE dot dot forward slash task manager to switch over to that folder and I'll just use clear
to clear the terminal output so we know how to create documents we can use insert one and insert many.
We know how to read documents we can use find and find one.
And for update we use UPDATE 1 and update many.
Let's go ahead and explore them down below.
I'm gonna remove all the code for find once again that's always available inside of the PD f guide for
that lecture and down below.
We're gonna start to update our documents to start let's go ahead and switch up someone's name.
So inside of our database I have a collection of users.
And right here as an example I have to Andrews I'm going to go ahead and adjust one of those to be something
else.
So what I'm going to do is target this second Andrew by I.D. and I'll update his name to something like
Mike over in the API docs.
We have two of the methods we'll be using for collection if we scroll down near the end for you we have
Update 1 for updating one document and update many for updating multiple.
There's also this update method that's been deprecated.
It's an old way to update documents and it's going to be removed in a future version.
You should either be using update many or update 1 depending on what you're trying to do.
Now we'll start with UPDATE 1 to update just that one document.
The first object we provide is the filter.
So we target the one document we're trying to update we'll target it by I.D. so we could target it by
any other field value.
Then we have the updates we're trying to apply.
Now there's also this optional options object which we're going to skip like we've been doing so far.
And we have our callback as the third argument now for this.
We're actually not going to use a callback.
We're gonna use a promise.
And down below I can see under its return value it returns a promise if no callback is passed in.
Let's go ahead and actually use this first up I do need to grab the idea of the document I'm trying
to update I'm gonna go ahead and right click this document to click edit so I can copy the I.D. value
then I'll bring that over to my application code.
So over here we're gonna go ahead and use Update 1 and to update just that one document.
Now it still starts off these same way.
So D.B. dot collection I'm trying to update a document in that user's collection and I'm gonna go ahead
and use the update one method which we just explored.
Now the first object this is where we provide the search criteria.
So in our case we're going to target the document by its I.D. and the I.D. is going to equal a new object
I.D..
And we're gonna go ahead and pass to it as its first and only argument the string value we copied from
robo.
3 T.
So in this case we're creating an object I.D. using an existing value.
And that's how we're gonna be able to target our documents accordingly.
Now that we're targeting the document correctly we can go ahead and move on to the second argument for
Update 1.
This is the actual updates we want to apply.
Now this is where things get a bit different from what you might expect.
We don't just change things right here like setting name equal to Mike.
Instead we use what are known as update operators to define the behavior we want to perform.
We'll pull up the documentation for the update.
Operators in a moment.
Let's just start with the most common one which is dollar sign it's set.
And this operator allows us to set new values for the fields in our document.
And right here we're gonna go ahead and provide it an object value which is what it expects from here
we can customize the fields like name or age.
In this case I just want to customize the name of the document and I'll go ahead and set it to something
new.
That's not Andrew like Mike.
Perfect.
Now it's important to note that when we use set and we don't provide a field it's not going to delete
it.
In this case it's only impacting the fields we've explicitly listed out.
Nothing's happening to age.
Age is going to stay exactly as it is now if we were using the callback pattern we would go ahead and
provide a third argument to update 1.
This would be the function and we would get access to either the error or the result when we're using
promises though we don't provide that function.
Instead what Update 1 returns is indeed the promise.
So right here we can create a new variable update promise as an example and down below we can go ahead
and use some of the methods available on it right here.
That would be update promise then to register a function to run when things go well and the document
is updated and then down below we can set up a call to catch to register a function for when things
fail.
Now for the success case when the promise is fulfilled we'll get access to the result.
And when the promise is rejected we'll get access to the error and for both we'll just log them to the
terminal consult that log printing the result up above then down below.
The same thing console dot log printing the error.
Excellent.
Now when we run this we're actually going to see the name field update over in robo 3.
If I give things a quick refresh I can see the name still is Andrew down below.
I'm gonna run the script node Mongo D.B. dot J.
S to fire things off right here.
We got a bunch of output and then over inside of robo three T.
If we take things another refresh right here I can see the name has been updated to Mike so the output
we're getting is all of the things available on result.
And the only field we would ever pull off of there is modified count or possibly matched count matched
Count is always going to be 1 or 0 for our update.
One calls and modified count is going to be 1 or 0 1 if a document was modified and 0.
If no document was modified so that's how we can use.
Update 1 to update a document.
Now we can also change this syntax.
What we don't have to do is create a variable update promise and then add those method calls on there.
We can actually change all of this together.
So what does that mean.
It means this variable can be deleted and down below we can add our dot then call right on to the end
of the closing parentheses for update 1.
So it's D.B. dot collection Dot.
UPDATE One dot then dot catch.
And this is a very common pattern.
We're going to see throughout the class and that you're going to see as you start to work with a synchronous
javascript whether that's on the front end or whether that's inside of node j.
As this is a very common promise pattern.
Now let's pull up the complete list of update operators so we can see what we have available to us.
Aside from just set what I'm going to do is head over to the browser and what we're looking for is actually
not part of the API documentation but we can crack open a new browser tab to find it right here.
What I'm gonna do is Google Mongo D.B. update operators.
This is going to bring us over to the correct article on the reference documentation.
Right here we have the list of update operators.
And if we scroll down a bit on this page it's going to bring us to a nice long list of operators which
start with the dollar sign.
So down below we have set which we already used and we have other nice ones as well.
For example unset to remove a specific field from a document or rename to rename a field.
We also have other ones that aren't quite as common including things like increment or ANC for short
which allows you to increment a number in a particular document.
We could use this as an example to increment the age.
So these are some of the various ways we can work with our documents though in almost all cases you'll
be using set.
If you're trying to change the field value which is the most common scenario.
The second most common would be to use increment.
So let's go ahead and explore that.
If we had over two robo three Ts we can see that Mike has an age of twenty seven.
Let's go ahead and increment that to 28 from the visual studio code.
Ed I'm gonna remove the set operator and we are going to use I N C instead.
Now we can actually click the INEC operator in the documentation to learn how it works if we click on
that it brings us over to some examples.
Here we can see that we provide the field and the amount we want to increment by.
So let's get that done.
Now the only field we can actually increment is the age and we can increment in a positive way by using
a positive number.
Or we can decrement by using a negative number.
In this case let's use one to increment the age by 1.
I'm going to save Mongo DB.
J S I'm going to rerun the script from the terminal down below.
And if we had over two robo three TI we should see that age changed right here it was twenty seven I
refresh things and it's now twenty eight.
So those update operators allow us to do a lot of interesting things without needing to do more heavy
lifting if we didn't have access to this operator we would have to read the document out of the database
figure out what the current age is increment it and then use another operation to update the document.
So that would be a fetch and an update with the increment operator.
We can do all of this at once saving resources and getting things done just a bit quicker.
Now it's challenge time it's going to be up to you to use update many which works very similarly to
update 1.
So instead of updating one it updates many as the name suggests.
So I'm going to comment out all of the code for our update one call.
So it doesn't keep incrementing that age though I'll leave it in place as a reference for the moment
and down below.
I'll paste in those challenged comments.
So your goal is to use update many to complete all of the tasks.
So we have our tasks collection over in robo 3D.
I'll give things a quick refresh to make sure we're looking at the latest data.
I have three tasks.
One is complete the other two are not we want to use the update many operation to update all documents
to be complete so when they're done I should see completed it true for everything.
Now we can do that in one call with just update many as opposed to multiple with.
Update 1.
And that's exactly what you're going to do as your challenge.
So step 1 Check out the documentation for update many to get comfortable with how it works.
Hint It's kind of be pretty familiar to what we just learned about with Update 1.
Then you're going to set up a call with the correct query and your updates using the necessary update.
Operator And then you're going to use the promised methods to set up these success and error handler
functions.
And finally you're going to test your work.
You should be able to run the file just a single time refresh things over in robo 3 T and see completed
true for everything.
Now with all of your tasks are already completed or you mess up and you need to get better sample data
you can just right click one of your tasks edit it and switch the completed value over to false and
rerun your script to make sure it's working.
All right take some time to knock that out test your work and when you're done come back and click play
how'd that go.
Let's kick things off with step number one.
Check out the documentation for update.
Many now it's not a method we've used before but it is similar to methods we've used before and the
goal of working through this section and through these challenges is not to talk about every single
thing that Mongo D.B. can do because the course would become two hundred hours long and most of it would
be terribly boring.
The goal here is to give you the stuff that's essential to creating real world applications and to give
you the tools and know how to explore the other things.
When you're ready so down below we can go over to the update.
Many docs and we can see that the first argument is our filter.
The second is our updates.
Then we have the options which we don't need.
And we have our callback which we're not going to provide because we get a promise back if no callback
was passed.
Let's go ahead and knock this out over here.
I'm gonna set up the call with the query and the updates.
That's D.B. dot collection.
We are updating the documents in tasks.
I'll be using update many and I'm going to provide those two arguments.
First up we are going to find all tasks where completed is equal to False that's complete.
Don't forget the d..
Otherwise it won't work as expected.
Perfect.
Then we're going to provide that second object.
This is the list of operations we want to perform now in our case we just want to set a field value
so I will use set and we are going to set completed equal it to true.
So find the docs where completed is false.
Set it over to a completed value of true.
That's step 2 step 3.
Use the promised method to set up our handlers dot then right here.
Here we will get access to the result and then down below we can add a call to catch where we will get
access to the error should that promise get rejected perfect.
Now if the promise is fulfilled what are we going to do.
We'll just dump that result again.
Actually we can just dump something like the modified count.
Now you didn't need to dump the same thing to the council if you just printed result.
That's perfectly fine right here result.
Dot modified count.
Perfect.
Next up down below for the error.
We will just print that in its entirety.
And now we can go ahead and test things out.
So we looked at the docs we set up the call.
We set up the promised handler methods and now it's time to test the work.
Now I'm going to pull up robo three ti one more time and give things are refreshed just to make sure
the data is what I expected.
We have two tasks with the completed value of false and from the terminal down below.
We'll run our script.
I'm going to run it.
I can see that I have two documents updated which does sound right.
And if I refresh things over in rowboat 3 T what do I see.
I see that all of my tasks are now completed.
Which is fantastic.
So there we go.
Challenge complete.
We were able to use this new method to achieve the desired result.
Now we can remove the challenge comments and that's where we're gonna stop for this one in the next
video.
We're going to move on to the final letter in crud.
I know how to create I know how to read and update.
It's time to learn how to delete documents when we're done with them or they're no longer needed.
We're gonna cover that next.
So let's jump right in.