1
00:00:07,360 --> 00:00:08,620
In this video.

2
00:00:08,620 --> 00:00:18,630
Let's talk about another way of relating data, and that is by referencing when we say by referencing,

3
00:00:18,640 --> 00:00:20,020
what does it mean?

4
00:00:20,260 --> 00:00:30,400
It simply means that we are going to save the ID of a document into a field and we use a special type

5
00:00:30,400 --> 00:00:39,550
called Mongoose dot schema, dot types, dot object ID, meaning that we are going to save the ID of

6
00:00:39,550 --> 00:00:46,720
a document, but for embedded we save the entire object into a field.

7
00:00:46,750 --> 00:00:49,660
As we see in this slide.

8
00:00:49,750 --> 00:00:58,190
So here comes the question when do we need to use the embedded and when do we need to use the reference?

9
00:00:58,210 --> 00:01:01,750
Here is the answer we use embedded.

10
00:01:01,780 --> 00:01:09,670
If you know that in future that particular field will not grow too much, for example, a product and

11
00:01:09,670 --> 00:01:10,450
a review.

12
00:01:10,450 --> 00:01:16,930
In that way we can use the embedded way, but a post and a command.

13
00:01:16,930 --> 00:01:24,720
We need to use the reference because a post can have millions comment as compared to a review.

14
00:01:24,730 --> 00:01:32,050
So the best way to go is to use the referencing, and the referencing is the most recommended way of

15
00:01:32,050 --> 00:01:33,820
relating documents.

16
00:01:33,820 --> 00:01:36,190
So let's see them in action.

17
00:01:36,280 --> 00:01:38,800
So back to Visual Studio code.

18
00:01:38,800 --> 00:01:44,080
I have cleaned up the previous code and now it's back to default.

19
00:01:44,230 --> 00:01:48,370
In your case, we can make a copy for future reference.

20
00:01:48,370 --> 00:01:53,410
So for the post schema, this what we have the title and the contents.

21
00:01:53,440 --> 00:01:55,030
Likewise the user.

22
00:01:55,030 --> 00:02:00,000
We have the full name and then the country and inside the post.

23
00:02:00,010 --> 00:02:00,640
Right.

24
00:02:00,790 --> 00:02:05,870
You can see that this is a normal way of saving the command into MongoDB.

25
00:02:05,920 --> 00:02:10,570
Likewise, the function to there is nothing new here.

26
00:02:10,660 --> 00:02:13,120
Likewise the user too.

27
00:02:13,150 --> 00:02:20,350
You can see that we are saving a user document into DB and here we find the user.

28
00:02:20,350 --> 00:02:24,160
So these two modules are not related.

29
00:02:24,160 --> 00:02:28,230
So let's see how we can relate these two documents.

30
00:02:28,240 --> 00:02:29,770
Here is a clue.

31
00:02:29,980 --> 00:02:36,700
We are creating a blog application, meaning that a user can create many posts.

32
00:02:36,700 --> 00:02:39,370
So let's see how we are going to model that.

33
00:02:39,370 --> 00:02:42,820
So let's get back to the user model.

34
00:02:42,850 --> 00:02:50,470
So for this we are going to save the post, the user is going to create as part of it.

35
00:02:50,470 --> 00:02:52,690
Field So here we go.

36
00:02:52,720 --> 00:02:57,220
We bring in the post and here is the clue.

37
00:02:57,310 --> 00:03:02,170
This post must be an array, but what type of array?

38
00:03:02,170 --> 00:03:07,510
So let me show you here we bring in the array and then the object.

39
00:03:07,510 --> 00:03:21,400
And for this, the type is going to be as mongoose, dot schema, dot types, dot object ID and the

40
00:03:21,400 --> 00:03:24,970
next field is going to be the reference.

41
00:03:24,970 --> 00:03:27,610
So which model are we referencing?

42
00:03:27,610 --> 00:03:30,760
We are referencing the post model.

43
00:03:30,790 --> 00:03:37,600
Make sure that you type the exact name as the name of the model as we see here.

44
00:03:37,600 --> 00:03:41,140
So now we are done with the user.

45
00:03:41,530 --> 00:03:48,280
And also if you find a post, we want to know what user created that post.

46
00:03:48,340 --> 00:03:53,770
In that way we need to go to post and then add the author field.

47
00:03:53,770 --> 00:03:55,600
So here we go.

48
00:03:55,630 --> 00:03:59,560
We will add the author or the user for this.

49
00:03:59,560 --> 00:04:02,680
Let's make it as order for this.

50
00:04:02,680 --> 00:04:09,520
We can use the entire user because a post might have only one author.

51
00:04:09,640 --> 00:04:14,740
But the ideal way to go is to use the object ID.

52
00:04:14,860 --> 00:04:23,590
So the type here is going to be as we did for post mongoose dot schema, dot type dot object.

53
00:04:23,590 --> 00:04:26,660
ID take note about the common casing.

54
00:04:26,740 --> 00:04:33,310
And we also want to reference which model that is a user model and that is it.

55
00:04:33,310 --> 00:04:35,230
We are done with the relationship.

56
00:04:35,230 --> 00:04:39,520
So now let's get back to the root and create a post.

57
00:04:39,670 --> 00:04:48,850
So inside the client, let's go ahead and then register a user and now I'm going to use Joe and then

58
00:04:48,850 --> 00:04:57,550
country Ghana, I'll hit send and now you can see that we have a property called Post which is an array

59
00:04:57,550 --> 00:05:03,470
and by default it is empty because this user hasn't created a new post yet.

60
00:05:03,580 --> 00:05:06,520
Before we continue, let's finish up this one.

61
00:05:06,890 --> 00:05:13,910
This auto field must be a required fake, meaning that we need to look in before you can create.

62
00:05:13,910 --> 00:05:16,280
So let's add required to.

63
00:05:17,030 --> 00:05:21,110
But for a post on the user that I'm going to be optional.

64
00:05:21,140 --> 00:05:22,510
So here we go.

65
00:05:22,520 --> 00:05:25,370
So back to the post and points.

66
00:05:25,400 --> 00:05:31,100
Let's go ahead and create, but make sure that we copy the ID of the user.

67
00:05:31,130 --> 00:05:33,460
So let's go ahead and create that one.

68
00:05:33,470 --> 00:05:39,980
So back to the post route here and let's add the payload that we want to create.

69
00:05:40,010 --> 00:05:45,950
We need the title of the post and let me call this one as a HTML.

70
00:05:46,130 --> 00:05:55,550
And we need the content of the post and let's say the best posts ever.

71
00:05:55,640 --> 00:06:00,350
And when I hit send, you can see that I'm going to get some validation error.

72
00:06:00,380 --> 00:06:04,300
Simply mean that this sort of field is required.

73
00:06:04,310 --> 00:06:07,030
So let's go ahead and then do that one.

74
00:06:07,040 --> 00:06:10,500
So let's add a property called author.

75
00:06:10,520 --> 00:06:18,950
Well, if you're doing production application, we can get the author for the request object because

76
00:06:18,950 --> 00:06:20,450
we haven't done authentication.

77
00:06:20,450 --> 00:06:23,060
That's why I'm passing it manually here.

78
00:06:23,060 --> 00:06:24,470
So I'll pass that one.

79
00:06:24,470 --> 00:06:27,090
And next is our hit save.

80
00:06:27,110 --> 00:06:36,800
But before saving, well, we want to save the post this user has created into the field of this user.

81
00:06:36,800 --> 00:06:42,830
So before we save it, let's finish up the logic inside the post route.

82
00:06:42,830 --> 00:06:44,390
So here we go.

83
00:06:44,390 --> 00:06:47,410
So let's lay out the steps here.

84
00:06:47,660 --> 00:07:00,860
So step number one is we need to find the user by ID and remember we are passing the user as author

85
00:07:01,010 --> 00:07:03,940
so we can go ahead and then find the user.

86
00:07:03,950 --> 00:07:05,390
So here we go.

87
00:07:05,390 --> 00:07:07,070
Find the.

88
00:07:08,400 --> 00:07:18,180
User and as simple as cons user found is equal to our weight.

89
00:07:18,180 --> 00:07:30,530
And then the user model dot find by ID and I want to find buy rec dot body dot author where you can

90
00:07:30,660 --> 00:07:33,060
structure the body object.

91
00:07:33,270 --> 00:07:42,690
So here we can check that if there is no user found, then we can go ahead and then return and send

92
00:07:42,690 --> 00:07:51,630
some Jason data to the user by saying that message and user not found.

93
00:07:51,960 --> 00:07:59,930
Otherwise we can go ahead and then push the seatpost into the field of the user post.

94
00:07:59,940 --> 00:08:12,870
So next step is step number two is save the created post into the user's post failed.

95
00:08:13,320 --> 00:08:14,760
So here we go.

96
00:08:14,790 --> 00:08:19,600
Now on user found, we have a property called Post.

97
00:08:19,620 --> 00:08:20,670
Did you see that?

98
00:08:20,670 --> 00:08:26,760
And one that we are going to push the post ID for this one.

99
00:08:26,760 --> 00:08:29,670
We are going to save the entire post.

100
00:08:29,880 --> 00:08:33,870
It can happen that a post might have less say more properties here.

101
00:08:33,960 --> 00:08:37,980
But for this case we are just going to save the ID only.

102
00:08:38,100 --> 00:08:42,299
So for this we can use the entire object.

103
00:08:42,299 --> 00:08:51,870
And behind this scene Mongoose is going to use the ID only because we specify as an object ID or we

104
00:08:51,870 --> 00:08:56,220
can specify also as the underscore ID.

105
00:08:56,400 --> 00:09:04,100
Then the next step is that because we have made changes to the user documents we need to receive.

106
00:09:04,880 --> 00:09:05,490
Great.

107
00:09:05,490 --> 00:09:06,780
So here we go.

108
00:09:06,810 --> 00:09:07,680
And wait.

109
00:09:07,680 --> 00:09:13,420
And then user found dot C and that is it.

110
00:09:13,440 --> 00:09:17,880
So with this one, I can go ahead and then make the request.

111
00:09:17,880 --> 00:09:19,130
So check it out.

112
00:09:19,140 --> 00:09:26,180
When I hit st this time around I get some error because I'm not passing in the user.

113
00:09:26,190 --> 00:09:34,560
So here I'm going to be as the author and going to be as rec dot body dot author.

114
00:09:34,590 --> 00:09:36,780
So now moment of truth.

115
00:09:36,780 --> 00:09:38,280
Let's hit st.

116
00:09:38,280 --> 00:09:40,560
I guess something now is loading.

117
00:09:41,040 --> 00:09:44,600
Let's wait for and here we go.

118
00:09:44,610 --> 00:09:50,750
You can see that now we are waiting this post by a user.

119
00:09:50,760 --> 00:09:55,500
So if you go ahead and then fetch all users.

120
00:09:55,530 --> 00:09:58,200
Now let's click on fetch all users here.

121
00:09:58,230 --> 00:10:08,490
And this time around we have a field of post and we have the ID of the post, but not the entire document.

122
00:10:08,520 --> 00:10:10,770
How awesome it is.

123
00:10:10,920 --> 00:10:17,220
Again, let me prove to you before I end up this here, and like I said, that we can save the entire

124
00:10:17,220 --> 00:10:22,170
object and behind the scene, Mongoose is going to take the ID only.

125
00:10:22,290 --> 00:10:23,250
Let's save it.

126
00:10:23,250 --> 00:10:26,500
And now let's go ahead and create a new post here.

127
00:10:26,670 --> 00:10:28,200
This time around, they change.

128
00:10:28,200 --> 00:10:34,800
This one to note, J is now the same author Let's hit send.

129
00:10:35,590 --> 00:10:40,440
And I got the response if I'll fetch or uses.

130
00:10:40,450 --> 00:10:46,720
You can see that mongoose is smart by taking the ID from the entire object.

131
00:10:46,750 --> 00:10:48,090
How awesome it is.

132
00:10:48,100 --> 00:10:50,670
So here comes the another question.

133
00:10:50,680 --> 00:10:53,590
How will I know the real post here?

134
00:10:53,620 --> 00:10:55,350
Likewise the post.

135
00:10:55,360 --> 00:11:00,960
How will I know the actual user than using the ID as we did before?

136
00:11:00,970 --> 00:11:03,900
We are going to use the populate method.

137
00:11:03,910 --> 00:11:12,700
So now back to the user user route here on fetching, we are going to populate the posts.

138
00:11:12,700 --> 00:11:22,270
So here you pass in the post, make sure that this value or this variable is on the user model because

139
00:11:22,270 --> 00:11:24,910
we have it already as post.

140
00:11:24,910 --> 00:11:28,960
So now let's save it and let's fetch our user.

141
00:11:28,960 --> 00:11:35,890
And this time around we go back the to our post than the ID.

142
00:11:35,920 --> 00:11:41,050
So upon requesting that's where we are going to go to populate it.

143
00:11:41,050 --> 00:11:43,240
How awesome this one is.

144
00:11:43,450 --> 00:11:49,060
Likewise, the post may be asking How will I know which user created this post?

145
00:11:49,060 --> 00:11:57,040
So as we did for user, we are going to populate that one and we use what is called author on the post

146
00:11:57,040 --> 00:11:57,460
field.

147
00:11:57,460 --> 00:12:01,000
Let me show you before I make the request, which is this.

148
00:12:01,000 --> 00:12:07,170
So now let's save this one and now let's go ahead and then fetch all posts.

149
00:12:07,180 --> 00:12:12,730
Now this is the end point to fetch or post here and let's hit send.

150
00:12:12,730 --> 00:12:15,790
And now we have two posts.

151
00:12:15,790 --> 00:12:26,290
One is the HTML with the author as this and then the post number two as node and then the author as

152
00:12:26,290 --> 00:12:26,920
this.

153
00:12:26,920 --> 00:12:30,580
And you can see that this post caused the IDs.

154
00:12:30,580 --> 00:12:36,490
So how can we make public to this one unless we do what is called nesting population?

155
00:12:36,700 --> 00:12:43,660
So back to the post route here to be able to use the populate, you are going to change the way we are

156
00:12:43,660 --> 00:12:44,410
doing this.

157
00:12:44,410 --> 00:12:52,540
Well, for this one too, we can achieve the same thing by using object and providing a path and the

158
00:12:52,540 --> 00:12:56,230
value must be a property on the post.

159
00:12:56,230 --> 00:12:57,700
So going to be the same thing.

160
00:12:57,700 --> 00:13:04,780
So let's save it this time around you're going to get and we got the same or not we need to find the

161
00:13:04,780 --> 00:13:12,490
post this one so let's thank and you can see that we have the same result but for this one we have more

162
00:13:12,490 --> 00:13:17,530
control how we can populate what is called nesting document.

163
00:13:17,530 --> 00:13:19,810
So this is the post route.

164
00:13:19,900 --> 00:13:29,380
So here we bring in populate again and as an object and then we specify the path.

165
00:13:29,380 --> 00:13:37,720
And the path this time around is called post and we can go ahead and also specify the model and this

166
00:13:37,720 --> 00:13:39,850
one is post model.

167
00:13:39,940 --> 00:13:46,750
So save it and now let's make the requests and here we go.

168
00:13:46,750 --> 00:13:56,440
And this time around we go back the actual author and the post to we go back the actual post.

169
00:13:56,620 --> 00:14:03,280
So this is what we call referencing and this is the most recommended way.

170
00:14:03,280 --> 00:14:06,730
And I advise you to stick to this method.

171
00:14:06,820 --> 00:14:07,360
All right.

172
00:14:07,360 --> 00:14:14,350
In this video, let me show you one cool thing about Mongoose, and that is virtual properties.

