All right.
So in the last lesson we tackled the PUT request to a specific resource. In this lesson we're going to
look at its close cousin or maybe non identical twin brother.
And we're going to update a particular article but only the fields that we actually provided data for.
So as we saw in the last lesson, if we make a PUT request to a specific resource and we only provide
a value for one field then the other field that is neglected or we didn't provide a value for the PUT
request will quite literally interpret that as you wanting to make it null or nil or empty or doesn't
exist.
So in the cases where you only want to update a specific field in a specific document then the HTTP method
that you should be using is patch.
So let's go and add a PATCH request.
So we're going to delete that semicolon from the previous put and this one is going to be the patch
method.
Now in here we're going to have a function that has a req and res and we're going to open up our
curly braces.
Now inside this patch function we want to update a particular document in our article collection but
we only want to update the field that we provide.
So in this case the CRUD operation is still update and we're still going to be using the same update
method that we used in the last lesson when we wrote the code to handle PUT request.
But in this case when we call the Update method we no longer have that overwrite property set to true
and the other addition is that we now have this set flag which tells MongoDB to only update the fields
that we have provided updates for.
So let's see this in practice.
So inside our code we'll target the article model and we'll say update and then the first parameter will
be the condition which is going to be the same req.params.articleTitle and that if you
remember of course comes from over here.
So the route that the user used or the URL.
Now the next thing that we have to add are the updates and we're going to use this flag called set.
Now the way that the set operator works is that you put in the flag by typing a dollar sign, a set and
then a colon. And then what follows are the values for the fields that you want to update and you can
add in as many as you want.
So this is the way that you would structure it
and there's also some really good examples of how you would use it.
So I recommend having a read through this reference and I'll link to it in the course resources list
as well.
So essentially when we come back to our code what we want our code to look like is that if the client
only wanted to change the title then this part should read title: something else
let's say "Chuck Norris". And if we only wanted to update the content then it would be something like this.
And if we wanted to update the content and the title then it would look something like this.
Now how can we make that dynamic so that we let the client choose what it is that they actually want
to update?
Well, we know that when we send a request over, body-parser will pass the request and turn it into an
object.
So that means that if I added a value for a title and a value for content let's just call that TEST
and TEST, then in this case when that gets sent over to my server what I will see when I tap into or
log my req.body is it will basically be equal to a Javascript object that has two properties:
a title called TEST and another property called content which is also set to TEST and you can verify this
if you want to just by console logging.
But essentially this is what it would look like.
Now if I only had a title that I sent over in my request then it would simply look like this.
So as you can see the req.body object is pretty much what we want to go in here which will
allow the user to determine which fields they want to update and which fields they don't want to update.
So all we have to do inside here is to specify the req.body.
And now when a client sends over a PATCH request then body-parser will repass the request and it will
pick out the fields that they have provided updates to
and by using the set flag we're able to update our database for only the fields that have a new value
which is fulfilling our PATCH request's duty.
So now that we've completed this we're almost ready to test it.
All we have to do now is to simply add in our callback, so our function that can send an error.
And if there were no errors then we will simply res.send "Successfully updated article."
But if there were errors then we will res.send the errors.
So now let's go and close off all of the places where we need semicolons, hit save.
Now before we test it I just want to show you that I've updated our database so that we restore our
database to the previous values.
So my article on Jack Bauer now has the previous title and the previous content and I did this by simply
right clicking on my article entry and just editing the document by copy and pasting the values that
we had from the GitHub repopulate database page.
So, now that I have the original title and the original content it's time to give patch a spin using
Postman.
So I'm going to change my request to PATCH.
I'm going to leave my URL exactly the same targeting articles/Jack%20Bauer in my body this time
I am only going to change the title.
So if you have any key and value pairs left over from previously you can just click on the cross to
delete them so you get a fresh start.
But in my case the only property I want to update is the title and I'm gonna change that to just say
Chuck Norris.
So, all going well this should update my article on Jack Bauer to change the title to Chuck Norris believe
the content and everything else completely intact.
So let's go ahead and hit send and we get a successful message back
but the moment of truth is when we update our articles collection and when we take a look at our last
entry, you can see that the content, the id, everything else has been kept exactly the same
and the only change is the one where I provided a value for. And that is how the PATCH method should
work.