All right.
Onwards and upwards.
Let's crack on with the next one.
So in this case the client is sending a HTTP DELETE request to our /articles route and this
should delete all the articles in our collection.
So this is something we haven't done before using Express. But as you would imagine it's pretty similar
to GET and poST. It's simply app
.delete specifying the route and a callback.
So let's go ahead and write that in our code.
So just below our app.post we're going to add the app.delete. And inside here the route that we're
going to target is exactly the same as before
/articles with an "s" at the end.
So I'm just gonna paste that in here. And then we're going to add our callback with our req and res and
then open up a set of curly braces.
And inside these curly braces we will code up how our server will respond when the user makes that DELETE
request to the article's route.
So how do we delete all of our documents inside the articles collection using Mongoose?
So in Mongoose if you look through the documentation you might come across a method called deleteMany.
And here's how it might look.
So first we specify the model name
so that will correspond to the collection that we want to delete.
So in our case that will be article. And then we call the deleteMany method
and inside this method we specify two arguments.
The first one are the conditions on which we could filter through the collection to figure out what
to delete.
Now similar to how we did it with the find method, if we simply left out these conditions then delete
Many will delete all of the documents inside a particular collection that we specify here. And then next
all there is is a callback function where we can check to see if there were any errors or if it went
successfully.
So let's try this out in our code.
So inside here we're going to target the Article model which is going to correspond to the articles
collection that we want to delete from,
and then we write a dot and we write deleteMany, open some parentheses and we're going to ignore the
conditions,
so we're going to delete everything. And then we're going to add a callback and then we can check to
see if there were no errors.
Then we're going to res.send "Successfully deleted all articles."
But if there was an error then we're going to res.send the error of course.
And then we're going to close everything off with semicolons.
And now we're ready to test it again using Postman.
So even though previously using Postman we tried it using the post route,
I can change this to any of the verbs that we already specified in our server.
So for example if I wanted to use the GET request on the /articles, what would you predict
we get back? All right.
Now let's try it.
Hit send. And yeah, it gets back all of the current articles that we have in our articles collection.
It works exactly the same as when we did it through the browser
using that URL.
Now the browser can only do GET,
but as we saw in the last lesson using Postman we can also test out post, put, patch, delete and a whole
bunch of other things.
Now to keep it RESTful we're going to keep it only limited to these five verbs and we are now testing
the delete method.
So we're going to send a DELETE request to localhost:3000/articles and our
server hopefully should respond with the corresponding data.
Now because when I delete all of our documents just before that happens, I'm going to hit copy over here
after making that GET request.
Alternatively you can go into Robo 3T and where you have your articles, if you click on this little
button here which looks like a a document
you can also view all of your document and copy it because we're very soon going to delete all of it.
So let's go ahead and hit send and send that DELETE request.
Now you can see it says "Successfully deleted all articles."
And if I change that to a GET request now you can see that I'm getting back nothing.
And if we switch over to our Robo 3T and I click view documents, no records left.
So we have now deleted all the articles when a user makes a DELETE request to our articles route.