WEBVTT 0 00:00.600 --> 00:07.980 So now that we've covered how you might create new documents and new collections into our database using 1 00:08.000 --> 00:13.560 Mongoose and we also in the last lesson looked at how you might validate new data that's being entered 2 00:13.560 --> 00:14.970 into the database, 3 00:14.970 --> 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 00:21.000 --> 00:23.400 using Mongoose. 5 00:23.400 --> 00:30.150 If you remember previously we added in that Peach but we forgot to add an a name for it. 6 00:30.180 --> 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 00:35.370 --> 00:37.140 field as required 8 00:37.170 --> 00:38.240 when this happened. 9 00:38.640 --> 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 00:44.610 --> 00:53.340 of Peach. I'm going to comment out my fruit.safe and down here just below the find 11 00:53.340 --> 00:56.190 we're going to update our peach. 12 00:56.190 --> 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 01:01.800 --> 01:04.250 the method updateOne. 14 01:04.340 --> 01:11.730 The reason I know how to do this is again because of the documentation. Inside the API documentation 15 01:11.730 --> 01:13.400 for all of our models 16 01:13.560 --> 01:15.800 you can see there's findOne, 17 01:15.840 --> 01:18.170 there's also update, update 18 01:18.180 --> 01:23.910 Many and updateOne. You can use all three of these depending on what you need 19 01:24.000 --> 01:32.420 but the one that I've chosen is updateOne and it has three required parameters and one optional parameter. 20 01:32.490 --> 01:40.320 What we have to specify is the condition to filter upon, so which document do you want to update basically. 21 01:40.560 --> 01:44.660 And then the next one is what are you going to update. 22 01:44.700 --> 01:52.560 And finally it accepts a callback which allows us to log any errors if there are any. Inside our code 23 01:52.560 --> 01:53.300 here 24 01:53.550 --> 02:00.840 we've called fruit.updateOne and now we're going to specify our first parameter which is going to 25 02:00.840 --> 02:05.000 be the filter or what it is that we want to update. 26 02:05.070 --> 02:09.660 In our case, I want to update the record that has a particular ID 27 02:09.900 --> 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 02:17.370 --> 02:24.630 when we created this document. And that ID corresponds to the document that has the peach without a name. 29 02:24.660 --> 02:30.740 So if I copy that over and I paste it in here then I now have my query. 30 02:30.750 --> 02:34.240 So this is the item that I want to update. 31 02:34.290 --> 02:36.760 It has an ID that is specific to it 32 02:36.930 --> 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 02:43.920 --> 02:49.670 In this case I'm going to update the name field and I'm going to set it to Peach. 34 02:50.400 --> 02:56.400 And finally the third parameter is simply going to be a callback function which will log any errors 35 02:56.490 --> 02:57.550 if there are any. 36 02:57.570 --> 03:01.050 So if error, then we'll log the error 37 03:01.350 --> 03:11.490 but if there weren't any errors, then we'll just log "Successfully updated the document". 38 03:11.520 --> 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 03:21.780 --> 03:22.140 js 40 03:22.140 --> 03:28.030 again with that update and you can see now it says "Successfully updated the document" 41 03:28.320 --> 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 03:35.490 --> 03:37.760 see that we don't have any extra documents. 43 03:37.770 --> 03:39.410 We've still only got five. 44 03:39.720 --> 03:47.690 But this last one now has the name field filled and it now has the value of Peach. 45 03:47.700 --> 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 03:55.650 --> 03:56.910 and peach. 47 03:57.060 --> 04:02.610 All of our data is now updated and complete. Over here 48 04:02.620 --> 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 04:09.760 --> 04:14.720 actually matter because we can tap into every single property using the dot notation. 50 04:14.770 --> 04:21.040 So it'll search through this entire document and find the relevant piece of data for you just by writing 51 04:21.130 --> 04:21.910 that fruit 52 04:21.910 --> 04:29.120 .name. The final thing that we want to do is what if we wanted to delete a particular record. 53 04:29.570 --> 04:33.260 Let's say I'm going to delete this peach after all. 54 04:34.590 --> 04:38.370 Well then we could use the deleteOne method. 55 04:39.280 --> 04:43.480 I'm going to leave this as a challenge for you. Based off this documentation 56 04:43.480 --> 04:50.070 I want you to delete the peach entry in our fruits collection. If you succeed 57 04:50.230 --> 04:57.870 when I run in my Mongo shell the same fruits.find I should no longer see this last record. 58 04:58.030 --> 05:01.580 Pause the video now and see if you can complete this challenge. 59 05:04.040 --> 05:10.120 They've actually very helpfully included an example for you to show you how you might call this delete 60 05:10.160 --> 05:13.940 One function. And it only has two parameters: 61 05:13.940 --> 05:15.860 one is the conditions. 62 05:15.860 --> 05:18.970 This is again what exactly do you want to delete. 63 05:18.980 --> 05:23.640 You need it to match with something right> And then the second one is a callback. 64 05:23.650 --> 05:28.590 Again this allows us to log any errors that occurred when we were deleting the document. 65 05:29.390 --> 05:31.600 Let's implement that in our code. 66 05:31.790 --> 05:34.660 So I'm going to comment out our updateOne 67 05:35.000 --> 05:44.610 and just below it I'm going to say fruit.deleteOne. And the first parameter is of course the condition, 68 05:44.630 --> 05:46.360 what do I want to delete. 69 05:46.400 --> 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 05:56.060 --> 05:57.300 that's unique about it, 71 05:57.380 --> 06:01.480 but in this case I know that I have a document that's called Peach. 72 06:02.180 --> 06:08.350 Next I'm going to specify the callback function which only has an error as a parameter. 73 06:08.630 --> 06:11.520 And again I'm going to check if there was an error, 74 06:11.720 --> 06:20.910 I will flag the error but if there were no errors then I'm going to log successfully. 75 06:21.320 --> 06:26.430 "Successfully deleted the document". 76 06:26.520 --> 06:33.920 Now let's go ahead and run this code in our hyper terminal and see what happens. 77 06:36.460 --> 06:39.280 So now we've got successfully "delted" the 78 06:39.490 --> 06:42.160 document, a typo there from me. 79 06:42.370 --> 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 06:50.110 --> 06:56.500 And again if I switch over to my Mongo shell and run db.fruits.find we now only have four 81 06:56.500 --> 07:01.490 entries with that last one where the name matches Peach being deleted. 82 07:02.950 --> 07:08.590 The other function that we might want to use is to deleteMany because sometimes you might want to just 83 07:08.710 --> 07:14.920 clear out a collection or delete a number of documents that match a particular condition. 84 07:15.100 --> 07:21.220 If you remember previously when we were running our app.js, for a couple of times we didn't actually 85 07:21.220 --> 07:23.590 comment out the person.save. 86 07:23.890 --> 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 07:31.360 --> 07:39.680 into our database many many times. If you head over to your Mongo shell and you wrote "show collections" 88 07:40.250 --> 07:42.960 you can see we have our fruits and our people. 89 07:43.070 --> 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 07:51.540 --> 07:54.500 37. As a challenge 91 07:54.500 --> 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 08:02.030 --> 08:03.500 out this collection 93 08:03.500 --> 08:10.430 through the use of this deleteMany method. Pause the video and try to complete the challenge. 94 08:10.440 --> 08:12.640 This is the syntax that we need to follow. 95 08:12.660 --> 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 08:18.420 --> 08:25.140 going to pass in the required two parameters which is the condition which matches the document we want 97 08:25.140 --> 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 08:35.570 --> 08:44.090 now tap into the people's collection by typing person which is the name of the model that is linked 99 08:44.150 --> 08:47.650 to the people's collection. Person 100 08:47.670 --> 08:51.640 .deleteMany 101 08:51.840 --> 08:54.990 and then the first parameter is of course the condition. 102 08:54.990 --> 08:58.620 So we said that the name has to match John. 103 08:59.190 --> 09:03.140 And then the second parameter was just a callback with an arrow 104 09:03.540 --> 09:08.120 and if there was an error log the error 105 09:08.700 --> 09:12.940 else log "Successfully deleted 106 09:15.910 --> 09:18.740 all the documents". 107 09:18.750 --> 09:22.950 Now let's run this code and clear out our collection. 108 09:28.490 --> 09:33.740 So" successfully deleted all the documents" documents. 109 09:33.830 --> 09:38.360 And now if we do db.people.find again, you can see it's completely empty. 110 09:38.360 --> 09:45.290 We've deleted all of these records where the name matches with John. To ensure that we only have one 111 09:45.300 --> 09:45.810 John 112 09:45.900 --> 09:53.700 let's go ahead and save a single one of these. 113 09:53.990 --> 09:58.010 And now we should have a single John in our records 114 09:58.080 --> 10:03.660 and now let's comment this out again before we start adding more Johns into our people's collection.