WEBVTT 0 00:00.240 --> 00:00.960 All right. 1 00:00.990 --> 00:02.800 Onwards and upwards. 2 00:03.060 --> 00:06.030 Let's crack on with the next one. 3 00:06.060 --> 00:16.770 So in this case the client is sending a HTTP DELETE request to our /articles route and this 4 00:16.860 --> 00:20.850 should delete all the articles in our collection. 5 00:20.850 --> 00:27.600 So this is something we haven't done before using Express. But as you would imagine it's pretty similar 6 00:27.630 --> 00:30.810 to GET and poST. It's simply app 7 00:30.810 --> 00:34.530 .delete specifying the route and a callback. 8 00:34.920 --> 00:37.940 So let's go ahead and write that in our code. 9 00:37.980 --> 00:45.750 So just below our app.post we're going to add the app.delete. And inside here the route that we're 10 00:45.750 --> 00:49.120 going to target is exactly the same as before 11 00:49.140 --> 00:52.410 /articles with an "s" at the end. 12 00:52.410 --> 00:59.010 So I'm just gonna paste that in here. And then we're going to add our callback with our req and res and 13 00:59.010 --> 01:01.620 then open up a set of curly braces. 14 01:01.620 --> 01:09.180 And inside these curly braces we will code up how our server will respond when the user makes that DELETE 15 01:09.180 --> 01:11.650 request to the article's route. 16 01:11.700 --> 01:18.630 So how do we delete all of our documents inside the articles collection using Mongoose? 17 01:18.630 --> 01:26.460 So in Mongoose if you look through the documentation you might come across a method called deleteMany. 18 01:26.670 --> 01:29.190 And here's how it might look. 19 01:29.190 --> 01:32.020 So first we specify the model name 20 01:32.130 --> 01:35.850 so that will correspond to the collection that we want to delete. 21 01:35.850 --> 01:41.680 So in our case that will be article. And then we call the deleteMany method 22 01:41.880 --> 01:45.330 and inside this method we specify two arguments. 23 01:45.420 --> 01:52.710 The first one are the conditions on which we could filter through the collection to figure out what 24 01:52.710 --> 01:53.620 to delete. 25 01:53.760 --> 02:00.660 Now similar to how we did it with the find method, if we simply left out these conditions then delete 26 02:00.660 --> 02:08.610 Many will delete all of the documents inside a particular collection that we specify here. And then next 27 02:08.670 --> 02:13.890 all there is is a callback function where we can check to see if there were any errors or if it went 28 02:13.890 --> 02:14.940 successfully. 29 02:15.030 --> 02:17.890 So let's try this out in our code. 30 02:17.970 --> 02:26.430 So inside here we're going to target the Article model which is going to correspond to the articles 31 02:26.430 --> 02:28.890 collection that we want to delete from, 32 02:28.890 --> 02:36.870 and then we write a dot and we write deleteMany, open some parentheses and we're going to ignore the 33 02:36.870 --> 02:37.620 conditions, 34 02:37.650 --> 02:43.140 so we're going to delete everything. And then we're going to add a callback and then we can check to 35 02:43.140 --> 02:45.620 see if there were no errors. 36 02:45.810 --> 02:54.180 Then we're going to res.send "Successfully deleted all articles." 37 02:57.020 --> 03:01.550 But if there was an error then we're going to res.send the error of course. 38 03:02.300 --> 03:05.550 And then we're going to close everything off with semicolons. 39 03:05.870 --> 03:11.320 And now we're ready to test it again using Postman. 40 03:11.590 --> 03:17.580 So even though previously using Postman we tried it using the post route, 41 03:17.650 --> 03:22.900 I can change this to any of the verbs that we already specified in our server. 42 03:22.930 --> 03:30.430 So for example if I wanted to use the GET request on the /articles, what would you predict 43 03:30.430 --> 03:32.190 we get back? All right. 44 03:32.230 --> 03:33.910 Now let's try it. 45 03:33.910 --> 03:41.200 Hit send. And yeah, it gets back all of the current articles that we have in our articles collection. 46 03:41.200 --> 03:44.740 It works exactly the same as when we did it through the browser 47 03:44.740 --> 03:46.400 using that URL. 48 03:46.960 --> 03:49.170 Now the browser can only do GET, 49 03:49.240 --> 03:55.360 but as we saw in the last lesson using Postman we can also test out post, put, patch, delete and a whole 50 03:55.360 --> 03:56.690 bunch of other things. 51 03:56.770 --> 04:03.160 Now to keep it RESTful we're going to keep it only limited to these five verbs and we are now testing 52 04:03.190 --> 04:05.040 the delete method. 53 04:05.140 --> 04:11.140 So we're going to send a DELETE request to localhost:3000/articles and our 54 04:11.140 --> 04:15.920 server hopefully should respond with the corresponding data. 55 04:15.970 --> 04:22.780 Now because when I delete all of our documents just before that happens, I'm going to hit copy over here 56 04:23.620 --> 04:25.690 after making that GET request. 57 04:25.690 --> 04:31.840 Alternatively you can go into Robo 3T and where you have your articles, if you click on this little 58 04:31.840 --> 04:34.300 button here which looks like a a document 59 04:34.300 --> 04:41.080 you can also view all of your document and copy it because we're very soon going to delete all of it. 60 04:41.080 --> 04:45.250 So let's go ahead and hit send and send that DELETE request. 61 04:45.250 --> 04:49.290 Now you can see it says "Successfully deleted all articles." 62 04:49.420 --> 04:54.480 And if I change that to a GET request now you can see that I'm getting back nothing. 63 04:54.640 --> 05:01.780 And if we switch over to our Robo 3T and I click view documents, no records left. 64 05:01.780 --> 05:10.090 So we have now deleted all the articles when a user makes a DELETE request to our articles route.