1
00:00:00,370 --> 00:00:04,760
OK so now we have everything we need to do in order to add a cat in.

2
00:00:04,770 --> 00:00:08,650
Now we just need to write the code the code that looks like this.

3
00:00:08,700 --> 00:00:14,040
We're going to make a new cat and when we make a new cat We're going to pass in data.

4
00:00:14,160 --> 00:00:20,740
So I'm going to say name and let's go with George and his age.

5
00:00:20,760 --> 00:00:27,480
Let's go with an older cat 11 and temperament is grouchy.

6
00:00:27,930 --> 00:00:30,530
Just going off of my experience with cats.

7
00:00:30,990 --> 00:00:34,080
And then what we'll also do is save this to a variable.

8
00:00:34,560 --> 00:00:35,970
So we'll call it far.

9
00:00:35,970 --> 00:00:38,980
George equals new cat.

10
00:00:38,990 --> 00:00:39,980
Her name is George.

11
00:00:39,990 --> 00:00:42,510
Age 11 temperament is grouchy.

12
00:00:42,840 --> 00:00:45,390
So this doesn't actually added to the database just yet.

13
00:00:45,600 --> 00:00:51,090
There's another step which is George Dodd save.

14
00:00:51,930 --> 00:00:55,070
And once we save it will be added to the database.

15
00:00:55,080 --> 00:01:01,410
However there's a possibility that it won't save let's say that our Mangu process isn't running for

16
00:01:01,410 --> 00:01:07,000
some reason or the Internet is not working on a machine it might potentially not save.

17
00:01:07,080 --> 00:01:14,460
So rather than just saving it blindly what we can do is pass a callback function into save and this

18
00:01:14,460 --> 00:01:16,900
function will be called when the save is done.

19
00:01:16,980 --> 00:01:20,760
Whether it worked or not and it will give us two different things.

20
00:01:20,760 --> 00:01:28,320
The first is any potential error and the second is it must not call it George is the item that's returned

21
00:01:28,320 --> 00:01:29,470
to the item that was saved.

22
00:01:29,470 --> 00:01:30,780
It does call it cat.

23
00:01:31,290 --> 00:01:34,500
And what we can do is check if there's an error.

24
00:01:34,500 --> 00:01:37,710
We'll just do a concert vault log.

25
00:01:37,710 --> 00:01:43,370
Something went wrong just like that.

26
00:01:43,500 --> 00:01:48,210
Otherwise let's do a come to that log.

27
00:01:48,480 --> 00:01:57,620
We just saved a cat to the database and then we're also called Salt log the cat.

28
00:01:57,630 --> 00:01:58,340
All right.

29
00:01:58,620 --> 00:01:59,760
So there's a lot here.

30
00:01:59,940 --> 00:02:01,700
So let's walk through this one more time.

31
00:02:01,860 --> 00:02:03,920
Let's start with just the save.

32
00:02:03,930 --> 00:02:11,370
So what we're doing is trying to save George the cat to the database and repassing in this function

33
00:02:11,430 --> 00:02:13,810
that will be called once this is done.

34
00:02:14,040 --> 00:02:17,300
And this is a pattern that we're going to see all the time in mongoose.

35
00:02:17,400 --> 00:02:23,490
So we'll have things like that find and then you pass in a callback function to that and then we'll

36
00:02:23,490 --> 00:02:30,240
do the same thing for Dot remove and for Dot create in pretty much everything we do with Mongoose will

37
00:02:30,240 --> 00:02:35,070
involve a callback function and that's because this takes time.

38
00:02:35,070 --> 00:02:41,760
Javascript has to go and communicate with this process here and figure out if it went right or went

39
00:02:41,760 --> 00:02:43,720
wrong and it takes time.

40
00:02:43,800 --> 00:02:49,530
So we have to do the callback in order to have code execute after this is done.

41
00:02:50,010 --> 00:02:50,310
OK.

42
00:02:50,340 --> 00:02:52,400
So our callback takes two parameters.

43
00:02:52,470 --> 00:02:54,430
The first one is a potential error.

44
00:02:54,750 --> 00:02:59,570
And this will be empty hopefully but if not then we're going to cancel that log.

45
00:02:59,570 --> 00:03:00,640
Something went wrong.

46
00:03:00,990 --> 00:03:02,690
Otherwise we'll print out.

47
00:03:02,700 --> 00:03:06,980
We just added We just saved your cat to the database and we'll print out the cat.

48
00:03:07,650 --> 00:03:08,040
OK.

49
00:03:08,040 --> 00:03:09,190
A lot of stuff.

50
00:03:09,330 --> 00:03:10,680
So let's see how this goes.

51
00:03:10,950 --> 00:03:14,860
Let's try running this node cat stuff.

52
00:03:14,910 --> 00:03:16,260
Yes.

53
00:03:17,100 --> 00:03:17,740
OK.

54
00:03:17,880 --> 00:03:24,240
And we get we just saved the cat to the database and we got the cat that we printed out with this line

55
00:03:24,240 --> 00:03:24,650
here.

56
00:03:24,780 --> 00:03:29,470
And you can see that we have an I.D. temperaments Graci age 11.

57
00:03:29,550 --> 00:03:30,740
Name is George.

58
00:03:31,140 --> 00:03:37,140
And I just want to highlight here Kat is referring to what came back from the database because we also

59
00:03:37,140 --> 00:03:43,530
have George and we could also conc about LUGG George if we wanted to and we would see that it's slightly

60
00:03:43,530 --> 00:03:44,340
different.

61
00:03:44,340 --> 00:03:46,930
George is not what's coming from a database.

62
00:03:46,950 --> 00:03:52,860
George is what we have in javascript that we're trying to save to the database and that is what's being

63
00:03:52,860 --> 00:03:55,490
sent back from the database.

64
00:03:56,400 --> 00:03:58,150
So let me prove to you that this worked.

65
00:03:58,230 --> 00:04:06,960
We can open up our Mongo con. And then if we do show Digby's we now have cat app so we can use cat out

66
00:04:06,970 --> 00:04:07,690
.

67
00:04:08,820 --> 00:04:15,660
And then what we can do is show collections first and we have cats which was automatically created by

68
00:04:15,660 --> 00:04:23,530
pluralizing this and then let's just do a DVD cat stuff find all and we have our new cat here George

69
00:04:23,550 --> 00:04:24,160
.

70
00:04:24,720 --> 00:04:25,210
OK.

71
00:04:25,220 --> 00:04:28,650
Let's exit out and now let's create one more cat.

72
00:04:28,770 --> 00:04:37,140
So change the name of the cat from George to Mrs. Norris which is the cat from Harry Potter films his

73
00:04:37,140 --> 00:04:44,460
cat and I have no idea how old she is let's just say seven although probably much older than that.

74
00:04:44,640 --> 00:04:50,850
And her temperament will be evil although to be fair we could say that about every cat ever in the history

75
00:04:50,850 --> 00:04:51,520
of the world.

76
00:04:51,840 --> 00:04:57,000
And yes it still says George and that's fine because this doesn't actually impact anything.

77
00:04:57,000 --> 00:05:00,660
It's just the name of the variable on the javascript's side of things.

78
00:05:00,720 --> 00:05:03,540
But remember we're just writing javascript.

79
00:05:03,720 --> 00:05:05,180
But then we're saving it here.

80
00:05:05,340 --> 00:05:10,240
And it takes the data inside and sends that to Mongo and saves it in the database.

81
00:05:10,350 --> 00:05:11,920
But this has nothing to do with it.

82
00:05:12,000 --> 00:05:15,060
That's just how we refer to it instead of our code.

83
00:05:15,060 --> 00:05:16,460
OK so let's try the same thing.

84
00:05:16,500 --> 00:05:18,610
Try and save that cat again.

85
00:05:18,810 --> 00:05:28,710
Run node Julius or cat Stut Julius OK and we get we just save another catch to the database and the

86
00:05:28,760 --> 00:05:32,840
90 temperaments evil age seven name is Mrs. Norris.

87
00:05:32,910 --> 00:05:33,170
OK.

88
00:05:33,180 --> 00:05:36,410
So that's how we can add cats to the database.

89
00:05:36,510 --> 00:05:43,620
The next important feature is retrieving cats from the database and to do that we'll go back here and

90
00:05:43,620 --> 00:05:46,150
down here I'm going to show you a new method.

91
00:05:46,380 --> 00:05:52,050
And actually I'm going to comment this out otherwise every time we run this code we'll get a new cat

92
00:05:52,380 --> 00:05:54,500
in our database and that's fine.

93
00:05:54,510 --> 00:05:57,200
But I only can have one Mrs. Norris at a time.

94
00:05:57,210 --> 00:06:00,400
I don't want to have five or six of them running around in our database.

95
00:06:00,510 --> 00:06:02,280
So I'm going to come it out for now.

96
00:06:02,640 --> 00:06:11,190
So down here what we need to do is use the find method on cat which was our model we defined right here

97
00:06:11,190 --> 00:06:12,000
.

98
00:06:12,000 --> 00:06:17,260
So Cat dot find and we're just going to pass in an empty object.

99
00:06:17,430 --> 00:06:24,660
And then the callback function and this callback function is just like the other one where we take the

100
00:06:24,690 --> 00:06:26,350
error as the first parameter.

101
00:06:26,460 --> 00:06:29,660
If there is potential error and we'll check so we can start there.

102
00:06:29,820 --> 00:06:33,140
If there's an error let's to cancel that log.

103
00:06:33,380 --> 00:06:36,360
Oh no error.

104
00:06:36,390 --> 00:06:41,780
And then we'll also print out the error itself.

105
00:06:41,850 --> 00:06:47,760
Otherwise let's pass in this second parameter which is going to be the result of the dot find which

106
00:06:47,760 --> 00:06:52,950
would be hopefully multiple cats although potentially just one cat but we'll call it cats.

107
00:06:53,100 --> 00:06:53,910
Totally up to us.

108
00:06:53,910 --> 00:06:56,830
Just like this is up to us as well.

109
00:06:57,000 --> 00:07:02,460
Hopefully by now you're recognizing that anytime we have a callback it's completely up to us to decide

110
00:07:02,820 --> 00:07:05,190
what to name these parameters.

111
00:07:05,670 --> 00:07:12,840
So we'll do else cancel that log cats and let's just do that and we'll add another one in here just

112
00:07:12,840 --> 00:07:17,290
to make it easier to see all the cats.

113
00:07:18,800 --> 00:07:19,350
OK.

114
00:07:19,620 --> 00:07:22,790
And save that and let's see if this works.

115
00:07:22,980 --> 00:07:28,650
So we're going to run cat to find and repassing an empty object because we're not looking for any particular

116
00:07:28,650 --> 00:07:28,970
cats.

117
00:07:28,980 --> 00:07:35,550
We just want all of them and then repassing in the callback because remember this takes some time or

118
00:07:35,550 --> 00:07:36,530
it might not work.

119
00:07:36,540 --> 00:07:37,860
There might be an error.

120
00:07:37,950 --> 00:07:42,060
So this code is executed when this is done.

121
00:07:42,210 --> 00:07:42,530
All right.

122
00:07:42,540 --> 00:07:44,700
And I would check if there's an error you printed out.

123
00:07:44,700 --> 00:07:47,280
Otherwise you print out the cats.

124
00:07:47,280 --> 00:07:53,530
Let's now make sure you have everything else commented out except for the important stuff appear and

125
00:07:53,570 --> 00:07:57,510
we'll clear the console and then run our file.

126
00:07:57,540 --> 00:07:58,630
There we go.

127
00:07:58,740 --> 00:08:05,480
So we get our old cat conses that log and we get an array that has two different casts in it.

128
00:08:05,490 --> 00:08:06,120
Great.

129
00:08:06,120 --> 00:08:09,150
So I'm going to show you one more thing we can do to create a cat.

130
00:08:09,330 --> 00:08:15,640
Rather than doing it in two separate steps like we have here where we're making a new cat and then running

131
00:08:15,640 --> 00:08:15,690
.

132
00:08:15,690 --> 00:08:17,030
Save on that cat.

133
00:08:17,220 --> 00:08:18,450
Well we can also do.

134
00:08:18,540 --> 00:08:20,580
I'll come get this out for now.

135
00:08:20,580 --> 00:08:28,400
All of this actually is used a method create which is just like find so it's can create.

136
00:08:28,950 --> 00:08:33,210
And this is basically new and save all at once.

137
00:08:33,300 --> 00:08:37,820
So rather than making it new first and then saving it to making sure it works.

138
00:08:37,860 --> 00:08:43,140
We can just try it right away with Create which will make a new one and save it in a database.

139
00:08:43,200 --> 00:08:45,450
So let's do name.

140
00:08:45,750 --> 00:08:47,270
And I'm running out of ideas here.

141
00:08:47,310 --> 00:08:49,890
Let's just call a cat snow white.

142
00:08:50,310 --> 00:08:54,150
And age is 15 which is a number.

143
00:08:54,330 --> 00:09:00,870
And miraculously we found a cat whose temperament was nice.

144
00:09:01,050 --> 00:09:03,270
Nice is too strong a word.

145
00:09:03,270 --> 00:09:04,980
Let's just go with bland.

146
00:09:05,310 --> 00:09:06,030
There we go.

147
00:09:06,270 --> 00:09:11,000
So we save it and if we run this now it might it might not work.

148
00:09:11,080 --> 00:09:14,960
Well we'll have no way of knowing until we added the callback here.

149
00:09:15,690 --> 00:09:22,380
So again hopefully you're seeing this pattern are going to see it all the time and then we'll do our

150
00:09:22,380 --> 00:09:25,950
if ever and we'll just do this nice and quick.

151
00:09:25,990 --> 00:09:27,400
Cancel that log error.

152
00:09:27,810 --> 00:09:31,340
Else cancelled out log cat.

153
00:09:31,710 --> 00:09:38,070
And remember I call it cat because we are creating a single cat versus down here I called the cats but

154
00:09:38,100 --> 00:09:41,140
it's just a matter of what makes sense for a name.

155
00:09:41,190 --> 00:09:43,590
So in this case cats made more sense here.

156
00:09:43,610 --> 00:09:50,410
Cat makes more sense but we could also call it new cat or newly created Cat or returned cat everyone

157
00:09:50,450 --> 00:09:50,760
.

158
00:09:51,060 --> 00:09:52,130
OK so save.

159
00:09:52,380 --> 00:09:55,360
And let's see if this works.

160
00:09:55,380 --> 00:10:02,910
So now what we've done is first right here we've added this new cat that we created and we got it back

161
00:10:02,910 --> 00:10:07,110
from the database snow age 15 Blanche temperament.

162
00:10:07,110 --> 00:10:09,800
And then we also left this code we didn't commented out.

163
00:10:09,870 --> 00:10:16,980
So we also went and found all of the cats in the database and we got three different cats grouchy evil

164
00:10:16,990 --> 00:10:19,490
and bland and yes those could be their names.

165
00:10:19,560 --> 00:10:21,340
Technically that's their temperament.

166
00:10:21,450 --> 00:10:24,590
Their names are George Mrs. Norris and Snow White.

167
00:10:25,170 --> 00:10:25,660
OK.

168
00:10:25,770 --> 00:10:27,410
So we saw a lot of stuff here.

169
00:10:27,570 --> 00:10:32,800
Let's do a very quick recap and we shrink this down a bit and let's start at the beginning.

170
00:10:33,180 --> 00:10:38,980
So what does mongers Mongoose is what's known as an odium object data mapper.

171
00:10:39,000 --> 00:10:44,190
What it really means is that it's a way for us to write javascript inside of our javascript files like

172
00:10:44,190 --> 00:10:49,340
our app J.S. or express files and that javascript code will interact with our database.

173
00:10:49,560 --> 00:10:55,710
So it's basically a javascript layer on top of Mongo DB and it adds some extra fancy things in that

174
00:10:55,710 --> 00:10:56,930
we haven't really seen yet.

175
00:10:57,060 --> 00:11:02,370
But it has some some extra special methods that makes it easier on us and then we connected the cat

176
00:11:02,370 --> 00:11:05,480
app database was created for us because it didn't exist.

177
00:11:05,580 --> 00:11:08,740
But next time we connect and actually every time we've connected.

178
00:11:08,760 --> 00:11:14,060
Aside from the first time we ran the file we connected to the same cat app that already existed.

179
00:11:14,250 --> 00:11:20,640
And then we defined a schema which is a plan for what a cat looks like a name age and temperament.

180
00:11:20,640 --> 00:11:24,760
Now I didn't mention this but we don't have to have a name age and temperament.

181
00:11:24,810 --> 00:11:30,810
If we left them blank we wouldn't get any errors but we can add that functionality in and we will later

182
00:11:30,990 --> 00:11:35,700
for instance when we have a user signing up on our Web site we don't want them to be able to leave password

183
00:11:35,820 --> 00:11:37,300
blank email blank.

184
00:11:37,320 --> 00:11:38,910
We need both of them to be there.

185
00:11:38,940 --> 00:11:44,720
So there's a way to do that with Mongoose and then we have what I think is the most confusing line conceptually

186
00:11:45,180 --> 00:11:47,750
which is where we take the cat schema.

187
00:11:47,750 --> 00:11:48,880
The plan for a cat.

188
00:11:48,990 --> 00:11:54,960
And we compile it into a model which returns an object that has a bunch of methods so it knows about

189
00:11:54,960 --> 00:11:57,050
our plan name age and temperament.

190
00:11:57,270 --> 00:12:03,000
But it also adds in things like create and find and a whole lot more methods.

191
00:12:03,120 --> 00:12:08,130
So then we save it to cat with a capital C which is conventional to have it be a capital name there

192
00:12:08,220 --> 00:12:13,560
for our model and then we did things like making a new cat and then saving it.

193
00:12:13,680 --> 00:12:16,920
And the key thing is the callback or we have an error.

194
00:12:16,980 --> 00:12:18,500
And then the data coming back.

195
00:12:18,930 --> 00:12:23,540
And then we did create as well or we do cat create same exact idea.

196
00:12:23,580 --> 00:12:27,900
We have a callback and then we have another method down here find.

197
00:12:28,170 --> 00:12:30,900
So I'll be showing you a few more methods as we go.

198
00:12:30,900 --> 00:12:36,180
The point is not really that you memorized the exact syntax of the methods but just conceptually are

199
00:12:36,180 --> 00:12:38,030
clear on how this works.

200
00:12:38,070 --> 00:12:44,300
We install mongoose connect with database schema model and then we interact with the database.

201
00:12:44,310 --> 00:12:48,060
All right so that's it for now and the next video I'm going to show you how you can add mongoose to

202
00:12:48,060 --> 00:12:51,660
an express app and interact with the database inside of an express app
