Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
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
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
3
00:00:13,560 --> 00:00:14,970
into the database,
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
5
00:00:21,000 --> 00:00:23,400
using Mongoose.
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.
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
8
00:00:35,370 --> 00:00:37,140
field as required
9
00:00:37,170 --> 00:00:38,240
when this happened.
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
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
12
00:00:53,340 --> 00:00:56,190
we're going to update our peach.
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
14
00:01:01,800 --> 00:01:04,250
the method updateOne.
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
16
00:01:11,730 --> 00:01:13,400
for all of our models
17
00:01:13,560 --> 00:01:15,800
you can see there's findOne,
18
00:01:15,840 --> 00:01:18,170
there's also update, update
19
00:01:18,180 --> 00:01:23,910
Many and updateOne. You can use all three of these depending on what you need
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.
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.
22
00:01:40,560 --> 00:01:44,660
And then the next one is what are you going to update.
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
24
00:01:52,560 --> 00:01:53,300
here
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
26
00:02:00,840 --> 00:02:05,000
be the filter or what it is that we want to update.
27
00:02:05,070 --> 00:02:09,660
In our case, I want to update the record that has a particular ID
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
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.
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.
31
00:02:30,750 --> 00:02:34,240
So this is the item that I want to update.
32
00:02:34,290 --> 00:02:36,760
It has an ID that is specific to it
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.
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.
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
36
00:02:56,490 --> 00:02:57,550
if there are any.
37
00:02:57,570 --> 00:03:01,050
So if error, then we'll log the error
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".
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.
40
00:03:21,780 --> 00:03:22,140
js
41
00:03:22,140 --> 00:03:28,030
again with that update and you can see now it says "Successfully updated the document"
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
43
00:03:35,490 --> 00:03:37,760
see that we don't have any extra documents.
44
00:03:37,770 --> 00:03:39,410
We've still only got five.
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.
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
47
00:03:55,650 --> 00:03:56,910
and peach.
48
00:03:57,060 --> 00:04:02,610
All of our data is now updated and complete. Over here
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
50
00:04:09,760 --> 00:04:14,720
actually matter because we can tap into every single property using the dot notation.
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
52
00:04:21,130 --> 00:04:21,910
that fruit
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.
54
00:04:29,570 --> 00:04:33,260
Let's say I'm going to delete this peach after all.
55
00:04:34,590 --> 00:04:38,370
Well then we could use the deleteOne method.
56
00:04:39,280 --> 00:04:43,480
I'm going to leave this as a challenge for you. Based off this documentation
57
00:04:43,480 --> 00:04:50,070
I want you to delete the peach entry in our fruits collection. If you succeed
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.
59
00:04:58,030 --> 00:05:01,580
Pause the video now and see if you can complete this challenge.
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
61
00:05:10,160 --> 00:05:13,940
One function. And it only has two parameters:
62
00:05:13,940 --> 00:05:15,860
one is the conditions.
63
00:05:15,860 --> 00:05:18,970
This is again what exactly do you want to delete.
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.
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.
66
00:05:29,390 --> 00:05:31,600
Let's implement that in our code.
67
00:05:31,790 --> 00:05:34,660
So I'm going to comment out our updateOne
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,
69
00:05:44,630 --> 00:05:46,360
what do I want to delete.
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
71
00:05:56,060 --> 00:05:57,300
that's unique about it,
72
00:05:57,380 --> 00:06:01,480
but in this case I know that I have a document that's called Peach.
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.
74
00:06:08,630 --> 00:06:11,520
And again I'm going to check if there was an error,
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.
76
00:06:21,320 --> 00:06:26,430
"Successfully deleted the document".
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.
78
00:06:36,460 --> 00:06:39,280
So now we've got successfully "delted" the
79
00:06:39,490 --> 00:06:42,160
document, a typo there from me.
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.
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
82
00:06:56,500 --> 00:07:01,490
entries with that last one where the name matches Peach being deleted.
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
84
00:07:08,710 --> 00:07:14,920
clear out a collection or delete a number of documents that match a particular condition.
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
86
00:07:21,220 --> 00:07:23,590
comment out the person.save.
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
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"
89
00:07:40,250 --> 00:07:42,960
you can see we have our fruits and our people.
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
91
00:07:51,540 --> 00:07:54,500
37. As a challenge
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
93
00:08:02,030 --> 00:08:03,500
out this collection
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.
95
00:08:10,440 --> 00:08:12,640
This is the syntax that we need to follow.
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
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
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
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
100
00:08:44,150 --> 00:08:47,650
to the people's collection. Person
101
00:08:47,670 --> 00:08:51,640
.deleteMany
102
00:08:51,840 --> 00:08:54,990
and then the first parameter is of course the condition.
103
00:08:54,990 --> 00:08:58,620
So we said that the name has to match John.
104
00:08:59,190 --> 00:09:03,140
And then the second parameter was just a callback with an arrow
105
00:09:03,540 --> 00:09:08,120
and if there was an error log the error
106
00:09:08,700 --> 00:09:12,940
else log "Successfully deleted
107
00:09:15,910 --> 00:09:18,740
all the documents".
108
00:09:18,750 --> 00:09:22,950
Now let's run this code and clear out our collection.
109
00:09:28,490 --> 00:09:33,740
So" successfully deleted all the documents" documents.
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.
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
112
00:09:45,300 --> 00:09:45,810
John
113
00:09:45,900 --> 00:09:53,700
let's go ahead and save a single one of these.
114
00:09:53,990 --> 00:09:58,010
And now we should have a single John in our records
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.
11990
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.