0 1 00:00:00,600 --> 00:00:07,980 So now that we've covered how you might create new documents and new collections into our database using 1 2 00:00:08,000 --> 00:00:13,560 Mongoose and we also in the last lesson looked at how you might validate new data that's being entered 2 3 00:00:13,560 --> 00:00:14,970 into the database, 3 4 00:00:14,970 --> 00:00:20,730 now I want to cover the last parts of CRUD and briefly show you how you might update and delete data 4 5 00:00:21,000 --> 00:00:23,400 using Mongoose. 5 6 00:00:23,400 --> 00:00:30,150 If you remember previously we added in that Peach but we forgot to add an a name for it. 6 7 00:00:30,180 --> 00:00:35,370 This was allowed to happen because we didn't have the validation in place yet, we didn't set the name 7 8 00:00:35,370 --> 00:00:37,140 field as required 8 9 00:00:37,170 --> 00:00:38,240 when this happened. 9 10 00:00:38,640 --> 00:00:44,550 Let's use this opportunity to learn about how you would update data and let's give that document a name 10 11 00:00:44,610 --> 00:00:53,340 of Peach. I'm going to comment out my fruit.safe and down here just below the find 11 12 00:00:53,340 --> 00:00:56,190 we're going to update our peach. 12 13 00:00:56,190 --> 00:01:01,800 So we're going to again tap into that fruits collection through the fruit model and we're going to call 13 14 00:01:01,800 --> 00:01:04,250 the method updateOne. 14 15 00:01:04,340 --> 00:01:11,730 The reason I know how to do this is again because of the documentation. Inside the API documentation 15 16 00:01:11,730 --> 00:01:13,400 for all of our models 16 17 00:01:13,560 --> 00:01:15,800 you can see there's findOne, 17 18 00:01:15,840 --> 00:01:18,170 there's also update, update 18 19 00:01:18,180 --> 00:01:23,910 Many and updateOne. You can use all three of these depending on what you need 19 20 00:01:24,000 --> 00:01:32,420 but the one that I've chosen is updateOne and it has three required parameters and one optional parameter. 20 21 00:01:32,490 --> 00:01:40,320 What we have to specify is the condition to filter upon, so which document do you want to update basically. 21 22 00:01:40,560 --> 00:01:44,660 And then the next one is what are you going to update. 22 23 00:01:44,700 --> 00:01:52,560 And finally it accepts a callback which allows us to log any errors if there are any. Inside our code 23 24 00:01:52,560 --> 00:01:53,300 here 24 25 00:01:53,550 --> 00:02:00,840 we've called fruit.updateOne and now we're going to specify our first parameter which is going to 25 26 00:02:00,840 --> 00:02:05,000 be the filter or what it is that we want to update. 26 27 00:02:05,070 --> 00:02:09,660 In our case, I want to update the record that has a particular ID 27 28 00:02:09,900 --> 00:02:16,920 and I can see that ID in my Mongo shell and it's this string over here which was generated automatically 28 29 00:02:17,370 --> 00:02:24,630 when we created this document. And that ID corresponds to the document that has the peach without a name. 29 30 00:02:24,660 --> 00:02:30,740 So if I copy that over and I paste it in here then I now have my query. 30 31 00:02:30,750 --> 00:02:34,240 So this is the item that I want to update. 31 32 00:02:34,290 --> 00:02:36,760 It has an ID that is specific to it 32 33 00:02:36,930 --> 00:02:43,920 and it's this. The next thing that I can add is what it is that I want to update about it. 33 34 00:02:43,920 --> 00:02:49,670 In this case I'm going to update the name field and I'm going to set it to Peach. 34 35 00:02:50,400 --> 00:02:56,400 And finally the third parameter is simply going to be a callback function which will log any errors 35 36 00:02:56,490 --> 00:02:57,550 if there are any. 36 37 00:02:57,570 --> 00:03:01,050 So if error, then we'll log the error 37 38 00:03:01,350 --> 00:03:11,490 but if there weren't any errors, then we'll just log "Successfully updated the document". 38 39 00:03:11,520 --> 00:03:21,770 Now let's close everything off and hit save and let's go into our hyper terminal and run on node app. 39 40 00:03:21,780 --> 00:03:22,140 js 40 41 00:03:22,140 --> 00:03:28,030 again with that update and you can see now it says "Successfully updated the document" 41 42 00:03:28,320 --> 00:03:35,490 and if we go over to our Mongo shell and we look at all the fruits inside our fruit database, you can 42 43 00:03:35,490 --> 00:03:37,760 see that we don't have any extra documents. 43 44 00:03:37,770 --> 00:03:39,410 We've still only got five. 44 45 00:03:39,720 --> 00:03:47,690 But this last one now has the name field filled and it now has the value of Peach. 45 46 00:03:47,700 --> 00:03:55,470 So now if we run it again and it logs all of our fruits, you can see we now have apple, kiwi, orange, banana 46 47 00:03:55,650 --> 00:03:56,910 and peach. 47 48 00:03:57,060 --> 00:04:02,610 All of our data is now updated and complete. Over here 48 49 00:04:02,620 --> 00:04:09,760 you might see that the field Peach got added in right at the end to this document but the order doesn't 49 50 00:04:09,760 --> 00:04:14,720 actually matter because we can tap into every single property using the dot notation. 50 51 00:04:14,770 --> 00:04:21,040 So it'll search through this entire document and find the relevant piece of data for you just by writing 51 52 00:04:21,130 --> 00:04:21,910 that fruit 52 53 00:04:21,910 --> 00:04:29,120 .name. The final thing that we want to do is what if we wanted to delete a particular record. 53 54 00:04:29,570 --> 00:04:33,260 Let's say I'm going to delete this peach after all. 54 55 00:04:34,590 --> 00:04:38,370 Well then we could use the deleteOne method. 55 56 00:04:39,280 --> 00:04:43,480 I'm going to leave this as a challenge for you. Based off this documentation 56 57 00:04:43,480 --> 00:04:50,070 I want you to delete the peach entry in our fruits collection. If you succeed 57 58 00:04:50,230 --> 00:04:57,870 when I run in my Mongo shell the same fruits.find I should no longer see this last record. 58 59 00:04:58,030 --> 00:05:01,580 Pause the video now and see if you can complete this challenge. 59 60 00:05:04,040 --> 00:05:10,120 They've actually very helpfully included an example for you to show you how you might call this delete 60 61 00:05:10,160 --> 00:05:13,940 One function. And it only has two parameters: 61 62 00:05:13,940 --> 00:05:15,860 one is the conditions. 62 63 00:05:15,860 --> 00:05:18,970 This is again what exactly do you want to delete. 63 64 00:05:18,980 --> 00:05:23,640 You need it to match with something right> And then the second one is a callback. 64 65 00:05:23,650 --> 00:05:28,590 Again this allows us to log any errors that occurred when we were deleting the document. 65 66 00:05:29,390 --> 00:05:31,600 Let's implement that in our code. 66 67 00:05:31,790 --> 00:05:34,660 So I'm going to comment out our updateOne 67 68 00:05:35,000 --> 00:05:44,610 and just below it I'm going to say fruit.deleteOne. And the first parameter is of course the condition, 68 69 00:05:44,630 --> 00:05:46,360 what do I want to delete. 69 70 00:05:46,400 --> 00:05:56,030 Well I want to delete the document that has a name of Peach. You can also use the ID or anything else 70 71 00:05:56,060 --> 00:05:57,300 that's unique about it, 71 72 00:05:57,380 --> 00:06:01,480 but in this case I know that I have a document that's called Peach. 72 73 00:06:02,180 --> 00:06:08,350 Next I'm going to specify the callback function which only has an error as a parameter. 73 74 00:06:08,630 --> 00:06:11,520 And again I'm going to check if there was an error, 74 75 00:06:11,720 --> 00:06:20,910 I will flag the error but if there were no errors then I'm going to log successfully. 75 76 00:06:21,320 --> 00:06:26,430 "Successfully deleted the document". 76 77 00:06:26,520 --> 00:06:33,920 Now let's go ahead and run this code in our hyper terminal and see what happens. 77 78 00:06:36,460 --> 00:06:39,280 So now we've got successfully "delted" the 78 79 00:06:39,490 --> 00:06:42,160 document, a typo there from me. 79 80 00:06:42,370 --> 00:06:50,110 But when it loops through our data to find all of the documents, we no longer have a peach being shown. 80 81 00:06:50,110 --> 00:06:56,500 And again if I switch over to my Mongo shell and run db.fruits.find we now only have four 81 82 00:06:56,500 --> 00:07:01,490 entries with that last one where the name matches Peach being deleted. 82 83 00:07:02,950 --> 00:07:08,590 The other function that we might want to use is to deleteMany because sometimes you might want to just 83 84 00:07:08,710 --> 00:07:14,920 clear out a collection or delete a number of documents that match a particular condition. 84 85 00:07:15,100 --> 00:07:21,220 If you remember previously when we were running our app.js, for a couple of times we didn't actually 85 86 00:07:21,220 --> 00:07:23,590 comment out the person.save. 86 87 00:07:23,890 --> 00:07:31,300 And that means that every time we ran app.js, we created that same person John who's 37, got saved 87 88 00:07:31,360 --> 00:07:39,680 into our database many many times. If you head over to your Mongo shell and you wrote "show collections" 88 89 00:07:40,250 --> 00:07:42,960 you can see we have our fruits and our people. 89 90 00:07:43,070 --> 00:07:51,500 And if you say "db.people.find" to show us all the data in there, we have loads of Johns who are 90 91 00:07:51,540 --> 00:07:54,500 37. As a challenge 91 92 00:07:54,500 --> 00:08:02,030 I want you to delete all of the data in here where the name matches with John and I want you to clear 92 93 00:08:02,030 --> 00:08:03,500 out this collection 93 94 00:08:03,500 --> 00:08:10,430 through the use of this deleteMany method. Pause the video and try to complete the challenge. 94 95 00:08:10,440 --> 00:08:12,640 This is the syntax that we need to follow. 95 96 00:08:12,660 --> 00:08:18,420 We're going to tap into a particular model and we're going to call the deleteMany method. And then we're 96 97 00:08:18,420 --> 00:08:25,140 going to pass in the required two parameters which is the condition which matches the document we want 97 98 00:08:25,140 --> 00:08:35,570 to delete and the callback to log any errors. Down here let's comment out our deleteOne and instead let's 98 99 00:08:35,570 --> 00:08:44,090 now tap into the people's collection by typing person which is the name of the model that is linked 99 100 00:08:44,150 --> 00:08:47,650 to the people's collection. Person 100 101 00:08:47,670 --> 00:08:51,640 .deleteMany 101 102 00:08:51,840 --> 00:08:54,990 and then the first parameter is of course the condition. 102 103 00:08:54,990 --> 00:08:58,620 So we said that the name has to match John. 103 104 00:08:59,190 --> 00:09:03,140 And then the second parameter was just a callback with an arrow 104 105 00:09:03,540 --> 00:09:08,120 and if there was an error log the error 105 106 00:09:08,700 --> 00:09:12,940 else log "Successfully deleted 106 107 00:09:15,910 --> 00:09:18,740 all the documents". 107 108 00:09:18,750 --> 00:09:22,950 Now let's run this code and clear out our collection. 108 109 00:09:28,490 --> 00:09:33,740 So" successfully deleted all the documents" documents. 109 110 00:09:33,830 --> 00:09:38,360 And now if we do db.people.find again, you can see it's completely empty. 110 111 00:09:38,360 --> 00:09:45,290 We've deleted all of these records where the name matches with John. To ensure that we only have one 111 112 00:09:45,300 --> 00:09:45,810 John 112 113 00:09:45,900 --> 00:09:53,700 let's go ahead and save a single one of these. 113 114 00:09:53,990 --> 00:09:58,010 And now we should have a single John in our records 114 115 00:09:58,080 --> 00:10:03,660 and now let's comment this out again before we start adding more Johns into our people's collection.