1
00:00:00,690 --> 00:00:06,510
Welcome back this video I'm going to show you the other way of associating data which is by using object

2
00:00:06,510 --> 00:00:07,430
references.

3
00:00:07,620 --> 00:00:12,600
So before we get started I'm going to make a new file here in the same directory and I'll call this

4
00:00:12,600 --> 00:00:13,260
one.

5
00:00:13,540 --> 00:00:15,590
References taught J.

6
00:00:16,530 --> 00:00:22,500
And I'm going to just copy everything over into that file from our previous lesson and I'm going to

7
00:00:22,500 --> 00:00:23,560
alter this.

8
00:00:23,610 --> 00:00:28,310
The first thing I'll do is change the database that we use just so that we're not conflicting things

9
00:00:28,310 --> 00:00:30,730
and we're not overwriting what we just previously did.

10
00:00:30,900 --> 00:00:35,610
So I'm going to make the name blogg demo underscore too.

11
00:00:35,820 --> 00:00:36,410
All right.

12
00:00:36,540 --> 00:00:39,490
And then I'm going to go ahead and just get rid of a lot of stuff in here.

13
00:00:39,690 --> 00:00:43,410
Pretty much everything aside from our schema that we have in the models.

14
00:00:43,650 --> 00:00:48,230
And I'll start by talking about what it means to reference data versus embedding it.

15
00:00:48,480 --> 00:00:53,940
So what we did last time in the last video was we embedded data where we had a user that looks something

16
00:00:53,940 --> 00:00:57,700
like this with a name and there's an email in there as well.

17
00:00:57,720 --> 00:01:05,190
And then there is a POST array and each post in that array was actually stored inside the array just

18
00:01:05,190 --> 00:01:11,910
like this where title and content.

19
00:01:11,910 --> 00:01:12,790
There we go.

20
00:01:13,590 --> 00:01:20,270
And we have you know five six as many as we want and hear different posts inside the POST array.

21
00:01:20,570 --> 00:01:27,990
Now what I'm going to show you in this lesson is very similar except what we actually do is have a poster

22
00:01:27,990 --> 00:01:33,380
array and inside all that we're going to store in here rather than the actual posts.

23
00:01:33,570 --> 00:01:37,980
We're going to have a bunch of IDs so we'll have these long IDs that look like that

24
00:01:41,430 --> 00:01:42,120
and so on.

25
00:01:42,270 --> 00:01:46,250
And these ideas will correspond to individual posts.

26
00:01:46,250 --> 00:01:55,230
So then we'll have a post over here that has ID equal to this and it will have title you called something

27
00:01:55,230 --> 00:01:57,130
else and so on.

28
00:01:57,480 --> 00:02:03,980
So again the key difference is that we're storing IDs which is where the name reference comes from.

29
00:02:04,140 --> 00:02:09,520
We're referencing the ID of another post rather than embedding the entire post.

30
00:02:09,900 --> 00:02:11,180
OK so let's get started.

31
00:02:11,370 --> 00:02:17,280
And we have to make a few changes to our schema rather than having posts be an array of post schema

32
00:02:17,280 --> 00:02:17,570
.

33
00:02:17,610 --> 00:02:20,400
What we actually do make it an array.

34
00:02:20,490 --> 00:02:25,030
But we're going to pass an object in and the object is going to look like this.

35
00:02:25,080 --> 00:02:27,190
I mean spaces out just a little bit.

36
00:02:27,420 --> 00:02:37,580
And our object is going to have two main properties type and ref and type is going to be mongoose that

37
00:02:37,590 --> 00:02:47,090
schema types dot object id and ref will be post.

38
00:02:47,130 --> 00:02:49,020
So let's talk about what I just did here.

39
00:02:49,050 --> 00:02:55,740
I've said that in the user schema there's a post attribute and it's an array but rather than being an

40
00:02:55,740 --> 00:02:58,110
array of posts like we have here.

41
00:02:58,750 --> 00:03:00,870
Or it's an array of the post schema.

42
00:03:00,870 --> 00:03:03,570
It's actually an array of object IDs.

43
00:03:03,660 --> 00:03:06,080
And this is just the way that we write that syntax.

44
00:03:06,330 --> 00:03:11,890
It's a mongoose object id belonging to a post and we can save.

45
00:03:12,120 --> 00:03:16,320
And just to make sure we didn't do anything wrong we can run the file make sure we don't get an error

46
00:03:16,320 --> 00:03:17,300
.

47
00:03:17,310 --> 00:03:18,560
Great.

48
00:03:18,600 --> 00:03:23,520
Now let's get started by creating a single user that we can then add post to later.

49
00:03:23,640 --> 00:03:37,020
So do user create and this one will give a email of Bob at gmail dot com and a name will be Bob Filcher

50
00:03:37,410 --> 00:03:43,680
and we'll save and then we'll just leave it like that and we'll just assume that this is going to work

51
00:03:43,920 --> 00:03:45,770
rather than adding the callback in.

52
00:03:45,810 --> 00:03:48,830
So this is just going to create our first user.

53
00:03:48,900 --> 00:03:53,160
So let's run the file.

54
00:03:53,160 --> 00:03:53,700
All right.

55
00:03:53,880 --> 00:03:58,630
So we don't see anything but we could go into Mangu and check it out.

56
00:03:58,640 --> 00:04:11,370
So show Digby's use log demo underscore too and then we can do the users find and we have one user with

57
00:04:11,360 --> 00:04:12,070
his name.

58
00:04:12,150 --> 00:04:19,170
Bob Belcher email Bob at gmail dot com and posts that's empty because we haven't added any posts in

59
00:04:19,170 --> 00:04:19,740
yet.

60
00:04:19,920 --> 00:04:25,800
So let's get out of here and the next thing I want to do is add a post to this user so I'm going to

61
00:04:25,800 --> 00:04:31,620
get rid of that create statement and I'll come out so that you can see it and what I'll do is make a

62
00:04:31,620 --> 00:04:32,330
new post.

63
00:04:32,430 --> 00:04:38,080
So post doc create and we're making it independently of the user.

64
00:04:38,190 --> 00:04:49,530
So we're making that post and we'll give it a title of how to cook the best burger and then content

65
00:04:50,550 --> 00:04:57,940
and not just fill it with blah blah blahs of laws for now just like that and will save and that should

66
00:04:57,940 --> 00:05:05,650
make us a post and just to make sure this time let's add in the callback error and post and we'll just

67
00:05:05,650 --> 00:05:12,510
cancel that log post and we're not going to handle the error just for the sake of time and run the file

68
00:05:12,510 --> 00:05:13,260
.

69
00:05:13,980 --> 00:05:18,120
And we've now added a new post to the database with an ID.

70
00:05:18,160 --> 00:05:22,950
Remember this I.D. remember the whole thing 5 6 2 5 8 3 7 8.

71
00:05:22,960 --> 00:05:23,530
Now I'm just kidding.

72
00:05:23,530 --> 00:05:25,630
But but remember what it looks like.

73
00:05:25,780 --> 00:05:28,600
And then we have our content and then our title.

74
00:05:28,600 --> 00:05:28,920
All right.

75
00:05:28,930 --> 00:05:30,790
So let's get out of there.

76
00:05:30,810 --> 00:05:35,880
So what we want to do next is talk about how we can connect a post and a user using that ID.

77
00:05:36,310 --> 00:05:40,090
And to do that I'm actually going to go through the steps of making another post.

78
00:05:40,090 --> 00:05:42,230
So let's call this how to cook the best burger.

79
00:05:42,390 --> 00:05:51,280
Part Two and this will make a new post and once we save that post We're then going to find the user

80
00:05:51,280 --> 00:05:52,190
PABX.

81
00:05:52,260 --> 00:05:59,480
So we're going to do a user find and we'll do find one and we'll find your name.

82
00:05:59,760 --> 00:06:07,370
Let's do e-mail this time where e-mail is Bob at gmail dot com and then we'll run this code afterwards

83
00:06:08,120 --> 00:06:12,640
ever and then we'll found that we'll call this found user.

84
00:06:13,490 --> 00:06:14,250
OK.

85
00:06:14,350 --> 00:06:15,380
So a lot going on.

86
00:06:15,670 --> 00:06:18,760
So we're creating a post once the post has been created.

87
00:06:18,880 --> 00:06:24,850
We're finding a user in this case this user here that we've already created and then we're going to

88
00:06:24,850 --> 00:06:29,860
handle the error if we need to.

89
00:06:30,490 --> 00:06:35,800
Otherwise we're then going to add the post into the users posts.

90
00:06:35,930 --> 00:06:45,700
So it's found the user posts which is what we called it up here and we're going to push in push post

91
00:06:46,380 --> 00:06:53,100
and post is referring to the post that we just created that was safe's the database with title How to

92
00:06:53,110 --> 00:06:56,570
Cook the Vestberg or to content blah blah blah blah blah.

93
00:06:56,800 --> 00:06:58,450
We save it if it works.

94
00:06:58,560 --> 00:07:03,760
We have a post variable that storing the post and then refining the user.

95
00:07:03,900 --> 00:07:07,090
In this case the one with email Bob at gmail dot com.

96
00:07:07,210 --> 00:07:14,340
And then if we find it we're then going to add the new post into that users post array.

97
00:07:14,620 --> 00:07:15,880
Then let's save that.

98
00:07:16,060 --> 00:07:20,160
So afterwards we need to do another found user does save.

99
00:07:20,800 --> 00:07:31,990
And yes we need a another callback and this one will have error and let's call this data just to make

100
00:07:31,990 --> 00:07:33,450
it really clear what's happening.

101
00:07:33,580 --> 00:07:36,880
And in here I'm just going to again handle the error

102
00:07:39,610 --> 00:07:46,720
and then if there is no error going to cancel that log data.

103
00:07:46,790 --> 00:07:48,750
All right man we have a lot here.

104
00:07:48,760 --> 00:07:56,200
So create a post find the user and then take that post push it into the users posts save the user and

105
00:07:56,200 --> 00:07:59,120
then when that's done print out the data.

106
00:07:59,350 --> 00:08:03,010
Let's see what we get.

107
00:08:03,000 --> 00:08:03,910
All right.

108
00:08:04,140 --> 00:08:04,960
So it worked.

109
00:08:05,110 --> 00:08:08,640
Here's our user name Bob Belcher email.

110
00:08:08,640 --> 00:08:15,360
Here's the user id and then instead of posts all that we have is an array with one item and it's an

111
00:08:15,390 --> 00:08:21,500
object ID versus when we had the embed J.S. where we were working with embedded data.

112
00:08:21,550 --> 00:08:26,230
We had the entire post so that this array instead of it we actually saw the data.

113
00:08:26,380 --> 00:08:27,730
Now we're just seeing the ID

114
00:08:30,470 --> 00:08:31,390
so.

115
00:08:31,540 --> 00:08:33,210
So let's go add one more.

116
00:08:33,390 --> 00:08:35,100
So we'll do part 3.

117
00:08:35,400 --> 00:08:38,580
And the text of that one instead of blah blah blah.

118
00:08:38,700 --> 00:08:41,370
It's just going to be jibberish just so that it's easier to tell apart.

119
00:08:41,380 --> 00:08:45,370
And the consul will add that post.

120
00:08:45,370 --> 00:08:51,330
Now we have two object IDs here and each one corresponds to an individual post.

121
00:08:51,700 --> 00:08:52,660
OK.

122
00:08:53,010 --> 00:08:55,020
So that's how we can associate them.

123
00:08:55,140 --> 00:09:00,290
But now we need to figure out how do we take this object id and find the correct post.

124
00:09:00,580 --> 00:09:02,260
Let's say that we want to find a user.

125
00:09:02,500 --> 00:09:08,190
And then I want to find that users post as well and not just the IDs but I want all of the data.

126
00:09:08,560 --> 00:09:14,710
So I'm going to clear the console and I'm also going to comment out this behemoth here.

127
00:09:14,720 --> 00:09:21,630
And so what we want to do is find user and then find all posts for that user.

128
00:09:21,730 --> 00:09:23,990
And there's actually a way to do that in one go.

129
00:09:24,120 --> 00:09:25,840
So we need to find the user first.

130
00:09:26,010 --> 00:09:31,990
So that's user up find and we'll do find one again and then we need to pass and what we're looking for

131
00:09:32,230 --> 00:09:37,100
which will be email where it's Bob at gmail dot com.

132
00:09:38,250 --> 00:09:39,750
And then this is something new.

133
00:09:39,900 --> 00:09:41,400
So we haven't seen this before.

134
00:09:41,560 --> 00:09:51,190
Dot populate we're going to populate posts and then dot exec and I'll explain all this in just a moment

135
00:09:51,190 --> 00:09:53,550
.

136
00:09:55,920 --> 00:09:57,420
OK.

137
00:09:58,360 --> 00:10:02,920
So what we've done here we've written a longer query that has multiple pieces.

138
00:10:02,910 --> 00:10:04,570
So we're finding a user.

139
00:10:05,010 --> 00:10:12,210
And then we're changing this populate posts which will actually populate the field posts look up all

140
00:10:12,220 --> 00:10:19,500
those object IDs find the correct data and then stick it in the post array and then we run exec to actually

141
00:10:19,500 --> 00:10:20,580
start the query.

142
00:10:20,590 --> 00:10:21,720
So it's a little bit weird.

143
00:10:21,970 --> 00:10:23,220
We're finding one.

144
00:10:23,380 --> 00:10:28,870
But notice that we're not using a callback in here and we're changing up populate and then we're giving

145
00:10:28,870 --> 00:10:33,070
it Dot exec at the very end which will actually make all of this happen.

146
00:10:33,120 --> 00:10:38,000
So then we're passing callback into exec because that's when we're executing the code.

147
00:10:38,470 --> 00:10:39,920
And if we did this right.

148
00:10:39,970 --> 00:10:41,560
So let's check if there's an error.

149
00:10:41,560 --> 00:10:44,690
First Council that log error.

150
00:10:45,070 --> 00:10:53,590
Otherwise hopefully our user is going to have all the posts pre-populated inside the posts attribute

151
00:10:54,010 --> 00:11:00,400
so will cancel that log user and let's try running this.

152
00:11:00,390 --> 00:11:03,780
Make sure everything is commented out except for this.

153
00:11:03,970 --> 00:11:05,230
Let's run.

154
00:11:06,350 --> 00:11:06,940
And what do you know.

155
00:11:06,940 --> 00:11:07,610
It worked.

156
00:11:07,870 --> 00:11:12,610
So we get a user name is Bob Belsher email Bob at gmail dot com.

157
00:11:13,030 --> 00:11:15,730
But now post isn't just an array of object IDs.

158
00:11:15,750 --> 00:11:18,570
It actually has the full post in there.

159
00:11:18,580 --> 00:11:19,460
Both of them.

160
00:11:19,650 --> 00:11:24,070
So you can see it does have an idea what we have content and title content and title.

161
00:11:24,120 --> 00:11:29,360
So to summarize everything that we did we store data that it looks like this here with object IDs.

162
00:11:29,470 --> 00:11:37,710
You can see the two objects IDs and then we made a second query to retrieve the data and we filled in

163
00:11:38,080 --> 00:11:43,870
the post Saray with all the data using populate that exec.

164
00:11:44,050 --> 00:11:47,690
So you might be wondering why we would use one versus the other.

165
00:11:47,710 --> 00:11:52,220
When would we embed data versus when would we use object id references.

166
00:11:52,210 --> 00:11:56,530
And it's a really good and really important question that will address as we use them instead of our

167
00:11:56,800 --> 00:11:57,570
camp.

168
00:11:57,730 --> 00:12:00,490
The short answer is that it depends.

169
00:12:00,490 --> 00:12:04,510
And you can get by using either style and pretty much every situation.

170
00:12:04,500 --> 00:12:09,820
However there usually is one either using references or embedding data that is going to lend itself

171
00:12:09,850 --> 00:12:14,500
better to whatever you're doing and we'll talk about that as we go through your camp and we start to

172
00:12:14,500 --> 00:12:19,670
add in comments and users and we add in ratings and photos and all these different features.

173
00:12:19,720 --> 00:12:22,750
Some of them are going to be embedded and some of them are going to be referenced
