1
00:00:00,190 --> 00:00:06,510
Well come back in this video going to show you how we can set up an association a one to many association

2
00:00:06,540 --> 00:00:13,710
between two mongoose models by embedding data and we're going to be working with user and post.

3
00:00:13,710 --> 00:00:19,980
So this will be like a Facebook or blog app where we have a user and a user can have many posts but

4
00:00:19,980 --> 00:00:26,520
a post belongs to one user and we're not going to be creating an actual app here with routes and templates

5
00:00:26,520 --> 00:00:26,790
.

6
00:00:26,790 --> 00:00:30,090
We're just going to focus on the data and the data modeling.

7
00:00:30,120 --> 00:00:36,110
So I'm going to get started by making a new file inside of this associations directory that I made.

8
00:00:36,300 --> 00:00:37,520
You can do this wherever you want.

9
00:00:37,530 --> 00:00:48,600
It's going to be a single file and I'll call it embed dot J.S. and I'll open that up and inside of here

10
00:00:48,960 --> 00:01:00,750
I'll require mongoose which I haven't installed yet so I'll do that next and PM install mongoose and

11
00:01:00,810 --> 00:01:02,150
that will take a moment.

12
00:01:02,160 --> 00:01:03,020
There we go.

13
00:01:03,360 --> 00:01:04,790
Let's make some space.

14
00:01:05,010 --> 00:01:11,250
And once we require it the next thing we want to do is connect to a database so mongoose does connect

15
00:01:12,750 --> 00:01:18,870
and this is totally up to us again the name of the database but it has to be Mangu D-B colon slash slash

16
00:01:19,380 --> 00:01:22,350
local host slash and roll call.

17
00:01:22,350 --> 00:01:26,890
This blog demo just like that and save.

18
00:01:27,500 --> 00:01:33,100
OK so now we need to define two models we have user and we have a post.

19
00:01:33,330 --> 00:01:38,610
The user will just have an e-mail and a name and a post.

20
00:01:38,640 --> 00:01:42,170
It's going to have a title and then a content.

21
00:01:42,540 --> 00:01:44,310
The actual post itself.

22
00:01:44,310 --> 00:01:47,330
So we'll start with the user need to define the schema.

23
00:01:47,520 --> 00:01:58,670
So far user schema equals new mongoose schema and then we're going to have email which is a string and

24
00:01:58,860 --> 00:02:01,790
name which is a string just like that.

25
00:02:02,100 --> 00:02:04,850
And then we'll create the model so far.

26
00:02:05,010 --> 00:02:11,760
User equals mongoose model user.

27
00:02:11,760 --> 00:02:16,340
The Cingular version and then user schema is what we're building it from.

28
00:02:16,530 --> 00:02:17,700
And we'll save that.

29
00:02:17,980 --> 00:02:18,640
OK.

30
00:02:18,930 --> 00:02:20,580
Now let's move on to the post.

31
00:02:20,640 --> 00:02:31,380
So we're going to create another schema this time var post schema equals new mongoose schema and then

32
00:02:31,410 --> 00:02:37,710
each post has a title that is a string and content that will just make a string as well.

33
00:02:38,340 --> 00:02:48,030
And then we also need to turn that into a model var post model equals mongoose stop model post comma

34
00:02:48,450 --> 00:02:51,500
post schema.

35
00:02:51,790 --> 00:02:52,590
Right.

36
00:02:52,650 --> 00:02:56,010
So we have two schema set up two models.

37
00:02:56,010 --> 00:02:57,670
Let's make a little bit of room here.

38
00:02:57,810 --> 00:03:00,830
So we have our posts and we have our users.

39
00:03:00,990 --> 00:03:02,610
And let's just try running this.

40
00:03:02,610 --> 00:03:09,100
Make sure we don't have any syntax errors to start node embed Dutcher Yes.

41
00:03:09,600 --> 00:03:11,090
Looks good.

42
00:03:11,100 --> 00:03:15,470
Now let's go and create a single user just to make sure our model works.

43
00:03:15,470 --> 00:03:21,630
So at the very bottom done here I'm going to make a new user so we'll just call it our new user equals

44
00:03:22,170 --> 00:03:35,220
new user and then we'll pass an email and email will be Charlie at around Edu and name will be Charlie

45
00:03:35,220 --> 00:03:37,240
Brown just like that.

46
00:03:37,380 --> 00:03:40,600
And then what saves a new user to save.

47
00:03:40,740 --> 00:03:46,230
And then we'll add in our callback with the error and then the data coming back which is hopefully our

48
00:03:46,230 --> 00:03:49,540
user and then not just you.

49
00:03:49,560 --> 00:03:55,370
If error cancels out log error.

50
00:03:56,670 --> 00:04:01,010
Else we're going to cancel that log user.

51
00:04:01,110 --> 00:04:03,340
So so far no associations.

52
00:04:03,390 --> 00:04:06,350
I'm just testing to see if our user model works.

53
00:04:06,360 --> 00:04:15,120
So let's run this node embed Dutchesse and we get our user back with the ID that was automatically generated

54
00:04:15,210 --> 00:04:17,190
name and email.

55
00:04:17,190 --> 00:04:22,110
Great let's try the same thing with post so comment that out so we don't get another user and we're

56
00:04:22,110 --> 00:04:31,980
going to do our new post equals new post and a Post has a title and the title of the post will be reflections

57
00:04:32,370 --> 00:04:37,060
on apples and the text of that post or the content.

58
00:04:37,050 --> 00:04:40,940
I think we called it yes content will be.

59
00:04:41,280 --> 00:04:43,620
They are delicious.

60
00:04:44,250 --> 00:04:44,640
All right.

61
00:04:44,640 --> 00:04:46,110
Great blog post there.

62
00:04:46,320 --> 00:04:53,520
We'll save make sure everything's good and then we're going to actually do a new post that save us and

63
00:04:53,520 --> 00:04:55,940
the callback again by now.

64
00:04:56,100 --> 00:05:01,520
Hopefully you're getting comfortable with this pattern because you're seeing it nonstop where are passing

65
00:05:01,530 --> 00:05:03,240
this call callback to mongoose.

66
00:05:03,360 --> 00:05:09,300
Every time we do anything with Monga is finding creating saving updating eventually removing eventually

67
00:05:09,600 --> 00:05:14,670
we need to pass in a callback with the error as the first parameter and then the data as the second

68
00:05:14,670 --> 00:05:15,360
one.

69
00:05:15,360 --> 00:05:15,590
All right.

70
00:05:15,600 --> 00:05:28,610
So if error cancels out log ever otherwise come to that blog post make sure these both work.

71
00:05:28,670 --> 00:05:32,260
Let's try it and we run into a small issue.

72
00:05:32,390 --> 00:05:36,920
It's telling us that post is not defined when we're trying to make a new post.

73
00:05:37,040 --> 00:05:41,990
And the most logical reason for that to happen would be if we named our model something other than post

74
00:05:41,990 --> 00:05:42,350
.

75
00:05:42,350 --> 00:05:43,480
And what do you know.

76
00:05:43,520 --> 00:05:48,100
We named it post model and by we I mean that I named it incorrectly.

77
00:05:48,410 --> 00:05:50,160
So let's go with post.

78
00:05:50,270 --> 00:05:53,800
We could have kept it as post model and then changed it here.

79
00:05:54,440 --> 00:05:58,760
But it's definitely conventional to have it just be the name of what you're making you don't need to

80
00:05:58,760 --> 00:06:03,920
have model in there you don't need any extra characters just the name of the model itself or of the

81
00:06:03,920 --> 00:06:06,900
entity post with a capital P..

82
00:06:06,920 --> 00:06:08,400
All right so that matches now.

83
00:06:08,460 --> 00:06:11,500
Right we have post and post here.

84
00:06:11,750 --> 00:06:17,740
Let's try this again and this time it adds a new post content.

85
00:06:17,750 --> 00:06:20,700
They are delicious title reflections on apples.

86
00:06:21,050 --> 00:06:25,340
OK now that we've made sure that our two models are working correctly we can make a post and we can

87
00:06:25,340 --> 00:06:26,150
make a user.

88
00:06:26,330 --> 00:06:31,550
But currently they have nothing to do with one another and we want them to have a relationship remember

89
00:06:31,550 --> 00:06:37,440
that we want a user to have or to own many posts and a post will belong to one user.

90
00:06:37,670 --> 00:06:39,830
So that's a one to many relationship.

91
00:06:40,020 --> 00:06:45,770
And the way that I'm going to show you to accomplish that is by embedding data instead of a user schema

92
00:06:45,780 --> 00:06:46,030
.

93
00:06:46,250 --> 00:06:47,520
So it's going to look like this.

94
00:06:47,550 --> 00:06:49,940
Then I'll explain it in just a moment.

95
00:06:50,090 --> 00:06:55,520
We're going to have a post's attribute inside the user and inside the Post's attribute.

96
00:06:55,520 --> 00:07:01,640
We're going to have a bunch of posts and to make that work we need to add the post schema inside of

97
00:07:01,640 --> 00:07:02,570
an array.

98
00:07:02,570 --> 00:07:08,210
So rather than making posts a string or a number or a boolean we're telling mongoose we want it to be

99
00:07:08,210 --> 00:07:14,640
a list an array of posts and we have to write post schema which is a little bit confusing honestly.

100
00:07:14,780 --> 00:07:19,510
I wish that it was just post the name of the model but it's just the way that it is.

101
00:07:19,520 --> 00:07:24,860
It needs to be the name of the schema and we have one other issue which is that if we run this right

102
00:07:24,860 --> 00:07:29,890
now it's not going to know what post-chemo is and that's because we're defining it second.

103
00:07:29,990 --> 00:07:33,500
So if we're embedding the data we need to define it first.

104
00:07:33,590 --> 00:07:39,770
So we define the post schema and the post model and then we define the user schema and inside the user

105
00:07:39,770 --> 00:07:45,560
schema we're adding an attribute called posts which is an array of posts.

106
00:07:45,560 --> 00:07:53,840
So if we do that our data will look something like this or we'll have a name for a user let's do e-mail

107
00:07:54,200 --> 00:08:00,960
and I'll just do jibberish a name and then we'll also have posts and posts an array.

108
00:08:01,220 --> 00:08:10,430
And inside that array we have multiple posts so each post has a title and content and a user can have

109
00:08:10,490 --> 00:08:13,380
many posts just like that.

110
00:08:13,730 --> 00:08:19,520
And so for every post that we get it will be a new post instead of the posts attribute on a particular

111
00:08:19,520 --> 00:08:20,870
user.

112
00:08:20,870 --> 00:08:22,030
So let's get that going.

113
00:08:22,160 --> 00:08:28,130
Let's create a user and then I'll show you how we can add a post to that user so we can just use our

114
00:08:28,150 --> 00:08:28,250
.

115
00:08:28,370 --> 00:08:29,990
Charlie Brown here.

116
00:08:30,020 --> 00:08:31,960
New User equals Charlie Brown.

117
00:08:31,970 --> 00:08:36,920
Although I'll change some of the e-mails and names because we already have Charlie Brown in the database

118
00:08:36,920 --> 00:08:37,480
.

119
00:08:37,490 --> 00:08:42,590
Let's go with are my any at Hogwarts edu.

120
00:08:43,100 --> 00:08:46,490
And then we'll change this to be my auntie Granger.

121
00:08:46,640 --> 00:08:47,190
All right.

122
00:08:47,330 --> 00:08:51,400
So we have a new user and then to add a post to that user.

123
00:08:51,650 --> 00:08:58,990
All they have to do is write new user posts that push because it's an array.

124
00:08:59,090 --> 00:09:08,980
And I push in a new post and a Post has a title and the title of my next post will be how to brew poly

125
00:09:08,970 --> 00:09:19,330
juice potion and then the content will be just kidding.

126
00:09:19,490 --> 00:09:23,540
The potions class to learn it all right.

127
00:09:23,540 --> 00:09:24,640
Whatever it doesn't really matter.

128
00:09:24,650 --> 00:09:31,810
So we have a title and some content and then we're going to push this post into new user which we defined

129
00:09:31,820 --> 00:09:32,450
up here.

130
00:09:32,810 --> 00:09:35,790
And the last thing we need to do is save new user.

131
00:09:35,840 --> 00:09:39,070
So a new user that save like that.

132
00:09:39,070 --> 00:09:46,280
And we actually already have this written so I'll just reuse that and uncomment that and this will try

133
00:09:46,280 --> 00:09:47,030
and save it.

134
00:09:47,150 --> 00:09:48,920
If there's an error it will let us know.

135
00:09:48,980 --> 00:09:54,440
Otherwise it will show us the user and then comment out all this stuff down here about posts and just

136
00:09:54,440 --> 00:09:56,880
save that and let's see what happens.

137
00:09:57,110 --> 00:10:02,720
So we have our post schema which you defined then we have the user schema and the new part is right

138
00:10:02,720 --> 00:10:06,340
here where we have an array of posts inside of the user.

139
00:10:06,740 --> 00:10:10,080
And then we're creating a new user and pushing into that post right.

140
00:10:10,550 --> 00:10:12,610
Let's give it a shot.

141
00:10:14,230 --> 00:10:14,690
OK.

142
00:10:14,780 --> 00:10:20,850
And we get back this user that we're printing here emails reminding at Hogwarts name it's Hermione Granger

143
00:10:21,020 --> 00:10:23,700
and then instead of posts we have a new post.

144
00:10:23,820 --> 00:10:25,600
So that's working just fine.

145
00:10:25,610 --> 00:10:30,650
Now let's try something else where I'm going to comment all this out and rather than creating a new

146
00:10:30,650 --> 00:10:33,450
user let's retrieve the existing user.

147
00:10:33,530 --> 00:10:35,350
So I'm going to retrieve her mining.

148
00:10:35,480 --> 00:10:38,570
So I'll do that with user find.

149
00:10:38,740 --> 00:10:40,660
And I'm going to use find one.

150
00:10:40,820 --> 00:10:44,760
Otherwise find will give me an array and I just want one thing back.

151
00:10:44,780 --> 00:10:52,190
So we're going to do you don't find one will find where name is Hermione Granger and then we'll do our

152
00:10:52,190 --> 00:10:58,180
standard callback error and we'll just call this user.

153
00:10:58,220 --> 00:11:00,670
Hopefully you're getting comfortable with that right now.

154
00:11:00,860 --> 00:11:05,990
This right here the callback that we write pretty much every time we do anything with Mongoose and then

155
00:11:05,990 --> 00:11:06,750
we'll just do it.

156
00:11:06,950 --> 00:11:07,970
If ever

157
00:11:10,660 --> 00:11:15,770
counsel dialog error and then Otherwise

158
00:11:20,030 --> 00:11:26,360
we'll constantly bug user and I have an extra bracket I need to get rid of every go.

159
00:11:26,360 --> 00:11:29,570
So this is just to find her my new back nothing new.

160
00:11:29,570 --> 00:11:34,430
We're just finding a new user and it works.

161
00:11:34,430 --> 00:11:36,200
We get the user coming back.

162
00:11:36,290 --> 00:11:39,030
Her name is her mind she e-mails her money at Hogwarts.

163
00:11:39,320 --> 00:11:44,690
OK now let's suppose I want to add another post because this is supposed to be a one to many.

164
00:11:44,720 --> 00:11:46,940
So one user can have multiple posts.

165
00:11:47,030 --> 00:11:48,680
Let's do that now.

166
00:11:48,710 --> 00:11:56,390
So inside of the else once we find the user We're going to run some code to add in a new post and then

167
00:11:56,390 --> 00:12:03,410
that looks like this user posts up push just like before and this time will push in another post so

168
00:12:03,410 --> 00:12:08,280
we'll push in title and the title is three things.

169
00:12:08,600 --> 00:12:16,490
I really hate and the content will be all the more all the more.

170
00:12:16,940 --> 00:12:18,150
And Voldemort.

171
00:12:18,350 --> 00:12:23,020
All right very creative post there remind me and we'll go ahead and save this.

172
00:12:23,060 --> 00:12:31,360
So we still need to do a user save and then we'll add in our callback afterwards.

173
00:12:31,460 --> 00:12:33,470
Another one of these error.

174
00:12:33,710 --> 00:12:36,440
And we'll just call this user again.

175
00:12:36,440 --> 00:12:47,660
And instead of here will do if error console log error otherwise cancel out log user.

176
00:12:47,660 --> 00:12:50,240
All right so this is what some people would call callback.

177
00:12:50,240 --> 00:12:52,570
Hell we have a lot of callbacks here.

178
00:12:52,670 --> 00:12:58,190
We have this first one which is finding her mind the finding that user that we're getting and then we're

179
00:12:58,190 --> 00:12:59,780
pushing in a new post.

180
00:13:00,090 --> 00:13:02,050
But that doesn't actually add to the database.

181
00:13:02,120 --> 00:13:03,600
We then have to save it.

182
00:13:03,800 --> 00:13:07,330
And so we do user save and this user right here.

183
00:13:07,400 --> 00:13:13,640
These two are referring to the user that comes back when we find her mind the the first time and then

184
00:13:13,640 --> 00:13:19,010
we're saving and then this user is referring to what comes back from the save.

185
00:13:19,040 --> 00:13:24,170
So this user here now has the new post added to it and save to the database.

186
00:13:24,290 --> 00:13:29,390
It's the data that is coming back to us from Mongo and then we're printing it out.

187
00:13:29,390 --> 00:13:30,340
Let's try it now.

188
00:13:30,500 --> 00:13:33,200
So let me get rid of this first counterplot log.

189
00:13:33,230 --> 00:13:34,890
Just leave it like that.

190
00:13:34,970 --> 00:13:40,380
Let's clear make sure we don't have any other code running that's going to interfere with our accounts

191
00:13:40,380 --> 00:13:41,400
about logs.

192
00:13:41,780 --> 00:13:43,970
Let's run it.

193
00:13:44,090 --> 00:13:44,750
All right.

194
00:13:44,930 --> 00:13:52,190
So what we see is that we're now printing out a single user Hermione that has two posts as first one

195
00:13:52,880 --> 00:13:59,180
and the second one and they're both stored inside of the post attribute on the single user.

196
00:13:59,180 --> 00:14:00,850
So it's one to many.

197
00:14:01,380 --> 00:14:01,660
OK.

198
00:14:01,670 --> 00:14:02,940
So that's all I want to show.

199
00:14:03,020 --> 00:14:04,890
Let's just reflect on this briefly.

200
00:14:04,940 --> 00:14:10,190
So we started by sitting at Mongoose and we have two models and they were independent to start.

201
00:14:10,370 --> 00:14:15,380
And then we associated them by adding this post schema array to the user schema.

202
00:14:15,590 --> 00:14:19,190
So now a user has an array of posts and it starts empty.

203
00:14:19,430 --> 00:14:27,110
But then we added to it by pushing in post dot push and then we saved in the next video going to show

204
00:14:27,110 --> 00:14:31,290
you another way of associating data using what are called object references.
