﻿1
00:00:01,110 --> 00:00:02,900
‫So remember how in the video

2
00:00:02,900 --> 00:00:05,510
‫about our data model, we said that

3
00:00:05,510 --> 00:00:07,880
‫we could either embed or reference

4
00:00:07,880 --> 00:00:10,204
‫the tour guide data, so in this video

5
00:00:10,204 --> 00:00:13,340
‫I'm gonna show you how we could implement embedding

6
00:00:13,340 --> 00:00:16,393
‫tour guide documents into a tour document.

7
00:00:17,930 --> 00:00:20,320
‫So again, in this lecture we are going to

8
00:00:20,320 --> 00:00:23,700
‫embed user documents into tour documents,

9
00:00:23,700 --> 00:00:25,840
‫and then in the next video, I will show you

10
00:00:25,840 --> 00:00:28,030
‫how we can actually reference users

11
00:00:28,030 --> 00:00:30,610
‫instead of embedding, all right?

12
00:00:30,610 --> 00:00:33,020
‫So the idea here is that when creating

13
00:00:33,020 --> 00:00:35,730
‫a new tour document, the user will simply

14
00:00:35,730 --> 00:00:38,630
‫add an array of user IDs, and we will then

15
00:00:38,630 --> 00:00:41,020
‫get the corresponding user documents

16
00:00:41,020 --> 00:00:42,827
‫based on these IDs, and add them

17
00:00:42,827 --> 00:00:44,800
‫to our tour documents.

18
00:00:44,800 --> 00:00:48,813
‫So in other words, we embed them into our tour, okay?

19
00:00:50,903 --> 00:00:52,323
‫So, let's do that here now.

20
00:00:55,430 --> 00:00:59,220
‫So guides will be of the type array, okay?

21
00:00:59,220 --> 00:01:01,700
‫Very simple, and actually before we implement

22
00:01:01,700 --> 00:01:04,080
‫any code, let me show you as an example

23
00:01:04,080 --> 00:01:06,943
‫how this would work when creating a new tour.

24
00:01:08,680 --> 00:01:11,360
‫So, let's go back to Postman here,

25
00:01:11,360 --> 00:01:14,523
‫and create new tour,

26
00:01:16,270 --> 00:01:18,330
‫okay, and so here in our body

27
00:01:18,330 --> 00:01:23,113
‫we of course would now add the guides,

28
00:01:24,410 --> 00:01:29,400
‫quotes, and then specify an array of IDs, okay?

29
00:01:29,400 --> 00:01:31,113
‫So, let's get some user IDs.

30
00:01:32,610 --> 00:01:36,070
‫So, get all users, let's see what we have here.

31
00:01:36,070 --> 00:01:38,070
‫So we have a guide here, actually,

32
00:01:38,070 --> 00:01:39,620
‫and so let's get this ID

33
00:01:40,460 --> 00:01:42,060
‫along with the quotes, actually,

34
00:01:46,370 --> 00:01:47,893
‫and so let's put that here,

35
00:01:49,410 --> 00:01:51,163
‫and let's add some other ones.

36
00:01:52,900 --> 00:01:57,100
‫So, this here as well and actually just these two.

37
00:01:57,100 --> 00:02:02,100
‫That should be enough for testing this stuff, okay,

38
00:02:02,410 --> 00:02:04,100
‫and so this is then how we're gonna create

39
00:02:04,100 --> 00:02:07,490
‫a new tour with two guides, okay,

40
00:02:07,490 --> 00:02:10,440
‫and again once we then save this tour,

41
00:02:10,440 --> 00:02:11,840
‫we will then, behind the scenes,

42
00:02:11,840 --> 00:02:13,472
‫retrieve the two user documents

43
00:02:13,472 --> 00:02:17,270
‫corresponding to these two IDs, all right?

44
00:02:17,270 --> 00:02:19,263
‫So, let's implement that,

45
00:02:20,440 --> 00:02:21,930
‫and back in our model

46
00:02:21,930 --> 00:02:23,960
‫the best place of doing that

47
00:02:23,960 --> 00:02:26,283
‫is a pre-saved middleware, right?

48
00:02:27,280 --> 00:02:29,980
‫So, that will then happen automatically

49
00:02:29,980 --> 00:02:31,550
‫behind the scenes, basically,

50
00:02:31,550 --> 00:02:34,730
‫each time that a new tour is saved,

51
00:02:34,730 --> 00:02:36,780
‫and actually, I don't want to do it here.

52
00:02:37,620 --> 00:02:39,250
‫So we have query, here,

53
00:02:39,250 --> 00:02:40,920
‫here's the document "Middleware,"

54
00:02:40,920 --> 00:02:42,203
‫so let's do that here.

55
00:02:43,320 --> 00:02:45,103
‫So tourSchema.pre,

56
00:02:53,580 --> 00:02:57,370
‫All right, let's call next right away here,

57
00:02:57,370 --> 00:02:59,600
‫so that we don't forget it in the end.

58
00:02:59,600 --> 00:03:04,600
‫Okay, so we get this.guides as an input,

59
00:03:05,220 --> 00:03:07,360
‫and remember this is gonna be an array

60
00:03:07,360 --> 00:03:10,330
‫of all the user IDs, right, and so we

61
00:03:10,330 --> 00:03:13,290
‫will loop through them using a .map,

62
00:03:13,290 --> 00:03:15,480
‫and then in each iteration get the

63
00:03:15,480 --> 00:03:18,373
‫user document for the current ID, all right,

64
00:03:19,477 --> 00:03:24,477
‫and we're gonna store that inside of guides, all right?

65
00:03:26,460 --> 00:03:29,284
‫So, the current element, which is gonna be the ID,

66
00:03:29,284 --> 00:03:30,840
‫well let's actually call it ID

67
00:03:30,840 --> 00:03:32,960
‫to make it a bit easier understand,

68
00:03:32,960 --> 00:03:36,360
‫and so now we actually need the user, right,

69
00:03:36,360 --> 00:03:38,803
‫so let's go ahead and import that.

70
00:03:41,630 --> 00:03:43,143
‫So, const User,

71
00:03:45,490 --> 00:03:46,563
‫require,

72
00:03:48,000 --> 00:03:53,000
‫and it's actually in the same folder here, all right,

73
00:03:55,759 --> 00:03:57,300
‫so User.findByID

74
00:04:01,350 --> 00:04:03,730
‫with the current ID, all right?

75
00:04:03,730 --> 00:04:05,550
‫But now, keep in mind that we actually

76
00:04:05,550 --> 00:04:09,977
‫do need to await this promise here, right

77
00:04:09,977 --> 00:04:11,870
‫and so, therefore, the function needs

78
00:04:11,870 --> 00:04:13,513
‫to be marked as async,

79
00:04:15,540 --> 00:04:18,313
‫but now we actually get a problem, all right,

80
00:04:18,313 --> 00:04:21,340
‫because the .map method will assign

81
00:04:21,340 --> 00:04:24,550
‫the result of each iteration to the new element

82
00:04:24,550 --> 00:04:27,150
‫in the guides array, okay, and so now

83
00:04:27,150 --> 00:04:29,100
‫we have an asynchronous function here

84
00:04:29,100 --> 00:04:31,760
‫and, as you know, that returns a promise,

85
00:04:31,760 --> 00:04:34,160
‫and so right now this guides array here

86
00:04:34,160 --> 00:04:38,120
‫is basically an array full of promises, okay.

87
00:04:38,120 --> 00:04:40,530
‫Let's actually call this guidesPromises

88
00:04:42,200 --> 00:04:44,160
‫and so we now actually need to run

89
00:04:44,160 --> 00:04:47,240
‫all of these promises, basically at the same time.

90
00:04:47,240 --> 00:04:49,520
‫As I showed you a bit earlier in this code,

91
00:04:49,520 --> 00:04:51,490
‫at least if you didn't skip the

92
00:04:51,490 --> 00:04:53,860
‫asynchronous JavaScript section,

93
00:04:53,860 --> 00:04:58,860
‫all we need to do is await Promise.all

94
00:05:01,230 --> 00:05:03,450
‫and then guidesPromise,

95
00:05:04,820 --> 00:05:07,670
‫okay, and we can directly assign the result

96
00:05:07,670 --> 00:05:11,390
‫of this to this.guides,

97
00:05:11,390 --> 00:05:14,130
‫and so basically override that temple array

98
00:05:14,130 --> 00:05:18,420
‫of IDs with an array of user documents, okay,

99
00:05:18,420 --> 00:05:20,000
‫and since we're now using await here,

100
00:05:20,000 --> 00:05:23,860
‫we need to mark this function as async, okay?

101
00:05:23,860 --> 00:05:27,900
‫Give it a save, and I think that's actually it, okay?

102
00:05:27,900 --> 00:05:31,460
‫So again, we need to use Promise.all here because

103
00:05:31,460 --> 00:05:35,298
‫the result of all of this here is a promise, okay,

104
00:05:35,298 --> 00:05:38,410
‫and so this array here is gonna be an array

105
00:05:38,410 --> 00:05:40,483
‫full of promises which we then run

106
00:05:40,483 --> 00:05:44,180
‫by awaiting Promise.all, okay?

107
00:05:44,180 --> 00:05:46,450
‫So, let's actually test this out now,

108
00:05:46,450 --> 00:05:50,018
‫and we already have all of this code here ready,

109
00:05:50,018 --> 00:05:52,373
‫we should just change this name here.

110
00:05:55,150 --> 00:05:58,560
‫So, 'Test Tour,' but the IDs are already here.

111
00:05:58,560 --> 00:06:01,760
‫So let's send this, and our tour name

112
00:06:01,760 --> 00:06:05,167
‫needs to be longer, 'New Task Tour.'

113
00:06:06,050 --> 00:06:11,050
‫Okay, but now let's take a look at our results,

114
00:06:11,240 --> 00:06:15,900
‫and so indeed we get our guides here, okay?

115
00:06:15,900 --> 00:06:18,580
‫So these are the complete documents,

116
00:06:18,580 --> 00:06:22,170
‫and indeed not just the IDs, right?

117
00:06:22,170 --> 00:06:24,198
‫Great, and so this is how we could

118
00:06:24,198 --> 00:06:28,440
‫implement embedding for this tour guides example.

119
00:06:28,440 --> 00:06:30,840
‫Now, this simple code that we implemented here

120
00:06:30,840 --> 00:06:33,850
‫of course only works for creating new documents,

121
00:06:33,850 --> 00:06:35,820
‫not for updating them, right?

122
00:06:35,820 --> 00:06:38,020
‫So now, we would have to do go ahead

123
00:06:38,020 --> 00:06:42,350
‫and implement this same logic also for updates.

124
00:06:42,350 --> 00:06:44,540
‫However, I'm not going to do that because,

125
00:06:44,540 --> 00:06:47,530
‫remember from the video where we modeled our data,

126
00:06:47,530 --> 00:06:49,450
‫that there are actually some drawbacks

127
00:06:49,450 --> 00:06:52,090
‫of embedding this data in this case.

128
00:06:52,090 --> 00:06:54,350
‫For example, imagine that a tour guide

129
00:06:54,350 --> 00:06:56,770
‫updates his email address, or they change

130
00:06:56,770 --> 00:06:59,250
‫their role from guide to lead guide.

131
00:06:59,250 --> 00:07:02,020
‫Each time one of these changes would happen,

132
00:07:02,020 --> 00:07:04,130
‫then you'd have to check if a tour

133
00:07:04,130 --> 00:07:06,610
‫has that user as a guide, and if so,

134
00:07:06,610 --> 00:07:08,670
‫then update the tour as well,

135
00:07:08,670 --> 00:07:10,650
‫and so that's really a lot of work

136
00:07:10,650 --> 00:07:11,640
‫and we're not gonna go

137
00:07:11,640 --> 00:07:13,085
‫in that direction, all right?

138
00:07:13,085 --> 00:07:15,530
‫Now, I still wanted to show you

139
00:07:15,530 --> 00:07:17,220
‫how embedding really works, and how

140
00:07:17,220 --> 00:07:19,520
‫we could implement it in this situation

141
00:07:19,520 --> 00:07:22,201
‫because I really believe it's very important

142
00:07:22,201 --> 00:07:24,551
‫that you still know how to do everything

143
00:07:24,551 --> 00:07:26,340
‫in case you ever need it

144
00:07:26,340 --> 00:07:28,590
‫in your own applications, okay,

145
00:07:28,590 --> 00:07:30,820
‫but in this particular situation we will,

146
00:07:30,820 --> 00:07:33,810
‫instead of embedding, use referencing,

147
00:07:33,810 --> 00:07:36,085
‫and I talked about reasons for doing so

148
00:07:36,085 --> 00:07:38,400
‫back in that video, okay?

149
00:07:38,400 --> 00:07:40,930
‫So, let's actually go ahead and do that

150
00:07:40,930 --> 00:07:42,803
‫right in the next lecture.

