1 00:00:00,210 --> 00:00:06,630 It's time to move on to that final crud operation which is D for delete now like with create read and 2 00:00:06,660 --> 00:00:12,930 update we'll be able to delete individual documents or we can batch delete multiple at the same time 3 00:00:13,350 --> 00:00:20,280 the two methods we're gonna use to get this done are under D for delete in the API docs we have delete 4 00:00:20,310 --> 00:00:25,680 many for deleting multiple and delete one for just removing an individual one. 5 00:00:25,680 --> 00:00:31,940 Now in the past the method you used to remove data was actually called remove which is down below. 6 00:00:32,010 --> 00:00:38,340 That's been deprecated and delete many and delete one are now the future of deleting data with Mongo 7 00:00:38,370 --> 00:00:39,170 D.B.. 8 00:00:39,360 --> 00:00:42,750 So we're gonna go ahead and kick things off with delete one. 9 00:00:42,780 --> 00:00:45,940 Now the API for both is very similar. 10 00:00:45,960 --> 00:00:49,670 We provide the filter to target the document or documents. 11 00:00:49,680 --> 00:00:50,780 We want to remove. 12 00:00:50,940 --> 00:00:54,840 Then we have the options which we're not going to provide as they're not needed. 13 00:00:54,840 --> 00:01:00,480 And we have the callback which we're also not going to provide since we'll use a promise instead. 14 00:01:00,870 --> 00:01:07,020 So for delete 1 and delete many all we really have to do is provide these search criteria similar to 15 00:01:07,020 --> 00:01:10,720 what we did with find and find one. 16 00:01:10,770 --> 00:01:13,140 Let's go ahead and move into Mongo D.B.. 17 00:01:13,260 --> 00:01:14,940 J asked to mess around with this. 18 00:01:15,030 --> 00:01:22,590 I'm going to remove our calls to update many and Update 1 and we will start by using delete many to 19 00:01:22,590 --> 00:01:26,030 delete multiple documents from that user's collection. 20 00:01:26,040 --> 00:01:30,910 What I'm going to do is remove all of the users who have the age of twenty seven. 21 00:01:30,990 --> 00:01:34,120 So right here if I refresh things there are two. 22 00:01:34,170 --> 00:01:38,460 I have Andrew and I have Gunther both with twenty seven as the age. 23 00:01:38,490 --> 00:01:43,170 My goal is to delete all of those with one single operation. 24 00:01:43,260 --> 00:01:47,930 So for Mongo D.B. J S will start off like we've done plenty of times before. 25 00:01:47,970 --> 00:01:55,530 D.B. dot collection we're looking for the user's collection and we will be using delete many to delete 26 00:01:55,560 --> 00:02:02,070 multiple documents and once again the only argument we're going to provide is the first one where we 27 00:02:02,070 --> 00:02:04,500 provide our search criteria. 28 00:02:04,500 --> 00:02:10,060 In this case we are looking to delete documents where the age is twenty seven. 29 00:02:10,080 --> 00:02:16,020 Now if I wanted to target two things I could add multiple properties onto here like for example deleting 30 00:02:16,050 --> 00:02:19,010 people whose name is Andrew and age is twenty seven. 31 00:02:19,140 --> 00:02:23,340 In this case we just want to delete based off of this one field. 32 00:02:23,340 --> 00:02:26,810 Now from here we can go ahead and set up our call to then. 33 00:02:26,850 --> 00:02:29,300 So right here we'll get access to the result. 34 00:02:29,340 --> 00:02:35,430 If we were able to perform the operation and we can also set up our call to catch and here we'll get 35 00:02:35,430 --> 00:02:36,830 access to the error. 36 00:02:36,840 --> 00:02:43,410 If we were not able to perform the operation and once again we'll just log both to the screen console 37 00:02:43,500 --> 00:02:50,860 dialogue printing result and then down below console dialog printing the error. 38 00:02:50,880 --> 00:02:53,240 Now with this in place we're actually all done. 39 00:02:53,250 --> 00:02:58,570 There's not a whole lot to deleting data and we can go ahead and run the script. 40 00:02:58,620 --> 00:03:05,310 What I'm gonna do is shut it down and using control C and then I'll start it up once again using up 41 00:03:05,370 --> 00:03:06,360 and enter. 42 00:03:06,360 --> 00:03:09,670 That's going to rerun things and we can see that on the result. 43 00:03:09,750 --> 00:03:15,000 We have a whole bunch of different pieces of information but I also have deleted account which is showing 44 00:03:15,030 --> 00:03:15,770 too. 45 00:03:15,780 --> 00:03:21,400 So this is the main result we get back allowing us to confirm the number of documents that were removed. 46 00:03:21,510 --> 00:03:28,320 Right here I can go ahead and refresh the user's collection and I can see that both of those are now 47 00:03:28,350 --> 00:03:28,950 gone. 48 00:03:29,190 --> 00:03:33,720 So there's no longer anyone with the age of 27 in the database. 49 00:03:33,720 --> 00:03:35,270 They've all been deleted. 50 00:03:35,310 --> 00:03:41,370 So with this in place we know how to use the delete many function to delete multiple documents. 51 00:03:41,370 --> 00:03:46,710 Now we're going to move on to the challenge for the video where it's up to you to use delete one to 52 00:03:46,710 --> 00:03:49,110 delete an individual document. 53 00:03:49,140 --> 00:03:51,990 Right here I have the challenge comments for this one. 54 00:03:51,990 --> 00:03:56,580 Your goal is to use delete one to remove a single task. 55 00:03:56,580 --> 00:04:00,320 First up you're gonna ground the description for the task you want to remove. 56 00:04:00,330 --> 00:04:06,450 So over inside of robo 3 T you could remove any of your tasks by their description. 57 00:04:06,450 --> 00:04:10,060 So I have these three right here as options for me. 58 00:04:10,080 --> 00:04:14,750 Now once you've picked the description go ahead and copy that to the clipboard and set up your call 59 00:04:14,760 --> 00:04:20,190 down below so you can refer to the documentation for delete one to figure out how it's supposed to be 60 00:04:20,190 --> 00:04:21,000 used. 61 00:04:21,000 --> 00:04:23,840 Hint It's basically the same as delete many. 62 00:04:23,940 --> 00:04:29,820 Now once you found that Colin place you are going to not provide the callback and instead opt for the 63 00:04:29,820 --> 00:04:30,630 promise. 64 00:04:30,630 --> 00:04:37,830 So toss on the then and catch method calls to set up your handlers and finally test your work rerun 65 00:04:37,830 --> 00:04:45,300 the script and make sure the one task you targeted is gone and that the others are still in place before 66 00:04:45,300 --> 00:04:46,220 I set you free. 67 00:04:46,230 --> 00:04:50,140 I will comment out our other call up above and down below. 68 00:04:50,190 --> 00:04:52,940 Right here you can add the challenge code. 69 00:04:53,030 --> 00:04:53,530 All right. 70 00:04:53,550 --> 00:04:54,420 Pause the video. 71 00:04:54,420 --> 00:04:57,540 Knock that out when you're done come back and click play 72 00:05:01,560 --> 00:05:02,320 out that go. 73 00:05:02,320 --> 00:05:06,700 Let's go ahead and kick things off by grabbing the description for the document. 74 00:05:06,910 --> 00:05:10,000 I'm gonna go ahead and remove clean the house. 75 00:05:10,000 --> 00:05:11,110 Now it's already done. 76 00:05:11,230 --> 00:05:13,780 So I don't need to keep it around right here. 77 00:05:13,780 --> 00:05:15,690 I'll grab its description. 78 00:05:15,760 --> 00:05:20,320 Next up I'm going to bring that over to my code and I'm going to call the correct method. 79 00:05:20,320 --> 00:05:23,750 That's going to be D.B. dot collection. 80 00:05:23,940 --> 00:05:31,290 I'm targeting that tasks collection and I will use delete one to delete just an individual item right 81 00:05:31,290 --> 00:05:31,920 here. 82 00:05:31,920 --> 00:05:39,810 I provide my query I am looking for the task where description equals a specific value. 83 00:05:39,850 --> 00:05:42,220 The string I just copied it to the clipboard. 84 00:05:42,220 --> 00:05:43,390 Perfect. 85 00:05:43,390 --> 00:05:49,530 Now from here we can go ahead and move on to the next step which is to add our promise method calls. 86 00:05:49,540 --> 00:05:55,250 Now there are so many ways we can structure code when we have a lot of chaining going on like this. 87 00:05:55,300 --> 00:05:58,780 I could take delete one and move that down to the next line. 88 00:05:58,780 --> 00:06:01,620 That would be a valid way to get the job done. 89 00:06:01,720 --> 00:06:08,080 Or I could go ahead and do something similar to what we've been doing before and simply break up the 90 00:06:08,080 --> 00:06:09,500 argument like this. 91 00:06:12,100 --> 00:06:15,980 And then toss on then setting up my callback right here. 92 00:06:15,980 --> 00:06:22,560 I'll get the result and I'll go ahead and dump that to the console and then down below that we'll make 93 00:06:22,560 --> 00:06:27,940 sure we use catch to handle those potential errors right here. 94 00:06:27,940 --> 00:06:29,870 I can call that E for short. 95 00:06:29,920 --> 00:06:32,780 Or we could just listed out in its entirety error. 96 00:06:32,950 --> 00:06:36,400 And once again I'll just log that. 97 00:06:36,430 --> 00:06:36,940 All right. 98 00:06:37,000 --> 00:06:37,600 All done. 99 00:06:37,600 --> 00:06:39,970 Let's go ahead and test things out. 100 00:06:40,000 --> 00:06:42,220 I'm gonna remove the challenge comments. 101 00:06:42,220 --> 00:06:47,680 I'm going to save the file and before I rerun it I'll just refresh the data to make sure clean the house 102 00:06:47,680 --> 00:06:51,280 is still showing up and it is from the terminal. 103 00:06:51,280 --> 00:06:53,220 I'll shut down the previous script. 104 00:06:53,220 --> 00:06:55,070 Start it up one more time. 105 00:06:55,090 --> 00:07:02,410 Right here I can see I have a delete count of one and if I refresh the tasks collection I can see that 106 00:07:02,410 --> 00:07:04,540 first document is gone. 107 00:07:04,540 --> 00:07:05,940 So there we have it. 108 00:07:06,010 --> 00:07:12,040 We now know how to create data read data update data and delete data. 109 00:07:12,040 --> 00:07:18,340 Now the goal of this section wasn't to start building out our next app which is that task manager REST 110 00:07:18,340 --> 00:07:19,240 API. 111 00:07:19,240 --> 00:07:25,930 The goal here was to just get comfortable working with Mongo D.B. and promises so we can use these together 112 00:07:25,930 --> 00:07:32,470 with Express to build out a more complete and real world application trying to learn about Mongo D.B. 113 00:07:32,470 --> 00:07:38,350 while also integrating it into an application would would've made it for quite a difficult journey. 114 00:07:38,350 --> 00:07:41,530 That's it for this video and that's it for this section. 115 00:07:41,530 --> 00:07:46,180 I'll see you in the section introduction for the next one which is coming up now.