﻿1
00:00:01,100 --> 00:00:03,700
‫Let's now populate the reviews

2
00:00:03,700 --> 00:00:06,503
‫with both the user and the tour data.

3
00:00:08,360 --> 00:00:11,470
‫So just like the did on the tour, let's now make is so

4
00:00:11,470 --> 00:00:14,910
‫that both the tour and the user will be automatically

5
00:00:14,910 --> 00:00:19,330
‫populated each time there is a query for a review.

6
00:00:19,330 --> 00:00:22,110
‫Since we already did that before, I will now

7
00:00:22,110 --> 00:00:25,140
‫give you yet another challenge, okay.

8
00:00:25,140 --> 00:00:27,140
‫So I want you to go ahead and do it

9
00:00:27,140 --> 00:00:31,060
‫just like we did in the tours, but now for the reviews.

10
00:00:31,060 --> 00:00:32,850
‫Just a hint that I need to give you

11
00:00:32,850 --> 00:00:35,140
‫before you can do this on your own,

12
00:00:35,140 --> 00:00:37,490
‫is that when you want to populate two fields,

13
00:00:37,490 --> 00:00:40,140
‫you need to actually call populate twice.

14
00:00:40,140 --> 00:00:43,430
‫So once for each of the fields, all right.

15
00:00:43,430 --> 00:00:45,740
‫With that information, you should be good to go

16
00:00:45,740 --> 00:00:48,763
‫{\an8}to pause the video now and go and try it.

17
00:00:52,780 --> 00:00:55,023
‫{\an8}So I hope that wasn't too hard.

18
00:00:56,070 --> 00:00:59,583
‫Let's go ahead and just close some files here,

19
00:01:00,850 --> 00:01:04,320
‫so everything that's not related to reviews,

20
00:01:04,320 --> 00:01:05,603
‫we don't need anymore.

21
00:01:06,740 --> 00:01:10,440
‫So let's just go to the review model and then

22
00:01:10,440 --> 00:01:12,823
‫implement, or pre-find middleware.

23
00:01:14,900 --> 00:01:18,120
‫So we do that on the schema, as you already know,

24
00:01:18,120 --> 00:01:21,870
‫and then again we use a regular expression

25
00:01:21,870 --> 00:01:25,883
‫to basically match strings, which start with find.

26
00:01:27,170 --> 00:01:30,100
‫So again, this is going to work for find,

27
00:01:30,100 --> 00:01:34,160
‫for find one, and all the other find one methods

28
00:01:34,160 --> 00:01:35,710
‫that are available in Mongoose.

29
00:01:40,960 --> 00:01:44,340
‫As always, I start by writing next.

30
00:01:44,340 --> 00:01:46,180
‫And now really all we need to do

31
00:01:46,180 --> 00:01:51,123
‫is to call populate on the current query, so this.populate,

32
00:01:53,200 --> 00:01:56,460
‫and I will now actually specify the options object

33
00:01:56,460 --> 00:01:59,550
‫because I only want to select a couple of fields

34
00:01:59,550 --> 00:02:02,573
‫and not the entire tour, and also not the entire user.

35
00:02:03,790 --> 00:02:07,540
‫So remember, we start with the path property,

36
00:02:07,540 --> 00:02:10,053
‫and this one is going to be for tour.

37
00:02:11,140 --> 00:02:15,280
‫So again, by specifying tour here, means that

38
00:02:15,280 --> 00:02:17,750
‫this field here, which has the exact some name,

39
00:02:17,750 --> 00:02:20,230
‫is then going to be the one that's populated

40
00:02:20,230 --> 00:02:21,860
‫based on a tour model.

41
00:02:21,860 --> 00:02:25,580
‫Well, because that's what we specified here, okay.

42
00:02:25,580 --> 00:02:28,530
‫So the reference is to a model called tour,

43
00:02:28,530 --> 00:02:31,680
‫and basically it's in that collection where Mongoose is then

44
00:02:31,680 --> 00:02:35,533
‫going to look for documents with the ID that we specified.

45
00:02:36,640 --> 00:02:41,290
‫All right, so we want a tour, but then

46
00:02:42,360 --> 00:02:45,070
‫let's say that we actually only want

47
00:02:45,070 --> 00:02:46,823
‫the tour name and nothing else.

48
00:02:47,830 --> 00:02:52,140
‫So select, okay, and then just the name,

49
00:02:52,140 --> 00:02:55,680
‫and then as I said, if we want to populate multiple fields,

50
00:02:55,680 --> 00:02:58,453
‫well all we need to do is to call populate again.

51
00:03:00,370 --> 00:03:03,950
‫So at this point the query is populated with the tours,

52
00:03:03,950 --> 00:03:05,650
‫and now we need to populate it again,

53
00:03:05,650 --> 00:03:07,123
‫this time with the user.

54
00:03:13,130 --> 00:03:15,110
‫And now select, and that's actually

55
00:03:15,110 --> 00:03:19,690
‫only display the user name and the photo,

56
00:03:19,690 --> 00:03:22,510
‫and not for example, stuff like the email.

57
00:03:22,510 --> 00:03:23,800
‫So let's say that someone hits

58
00:03:23,800 --> 00:03:26,140
‫the API to get all the reviews,

59
00:03:26,140 --> 00:03:28,720
‫but then we don't want to leak all the details

60
00:03:28,720 --> 00:03:32,200
‫about the users posting these reviews to the client.

61
00:03:32,200 --> 00:03:35,550
‫So no one should really be able to know the private data

62
00:03:35,550 --> 00:03:39,310
‫about the reviewers, like emails, okay.

63
00:03:39,310 --> 00:03:44,060
‫So again, we only leak, we only send relevant data about the

64
00:03:44,060 --> 00:03:48,720
‫user, and in this case, it's the name and the photo, okay.

65
00:03:48,720 --> 00:03:51,920
‫So give this a save, and if we now

66
00:03:51,920 --> 00:03:54,360
‫hit or get all reviews endpoint,

67
00:03:54,360 --> 00:03:58,070
‫well then already the tour and the user should be populated

68
00:03:58,070 --> 00:04:00,323
‫instead of us just seeing the IDs there.

69
00:04:01,320 --> 00:04:04,240
‫Let's try that, and here we go.

70
00:04:04,240 --> 00:04:07,260
‫So that's working, we get the name,

71
00:04:07,260 --> 00:04:11,890
‫and of course also the ID, and then the same for the user.

72
00:04:11,890 --> 00:04:14,783
‫So we get the name, but then not the photo.

73
00:04:15,780 --> 00:04:19,170
‫So let's see, maybe I didn't get, specify

74
00:04:19,170 --> 00:04:22,160
‫the photo property, so let's take a look at that.

75
00:04:22,160 --> 00:04:26,190
‫So the userModel, but actually it is there.

76
00:04:26,190 --> 00:04:29,900
‫No, I'm sorry, of course we do not see the photo

77
00:04:29,900 --> 00:04:33,463
‫because we never specified any photo for any of our users.

78
00:04:34,470 --> 00:04:38,230
‫Okay, and therefore the only property that actually exists

79
00:04:38,230 --> 00:04:41,550
‫is the name, and so that's what we then get.

80
00:04:41,550 --> 00:04:44,520
‫Okay, and that's actually it.

81
00:04:44,520 --> 00:04:47,550
‫So really simple, that's all we need to do

82
00:04:47,550 --> 00:04:49,020
‫to get all the related data,

83
00:04:49,020 --> 00:04:52,280
‫and just as we specified in our data model.

84
00:04:52,280 --> 00:04:53,900
‫Now again, just keep in mind

85
00:04:53,900 --> 00:04:56,690
‫that this one is going to add some extra queries,

86
00:04:56,690 --> 00:04:59,110
‫and in this case, it's actually two queries,

87
00:04:59,110 --> 00:05:01,410
‫because behind the scenes, Mongoose will actually

88
00:05:01,410 --> 00:05:05,200
‫have to query both the tours and also the users

89
00:05:05,200 --> 00:05:08,690
‫in order to find the matching document, okay.

90
00:05:08,690 --> 00:05:09,670
‫So this should then probably

91
00:05:09,670 --> 00:05:12,230
‫add some time here to the query.

92
00:05:12,230 --> 00:05:16,340
‫So it took 266 milliseconds, like this.

93
00:05:16,340 --> 00:05:19,700
‫Let's just, for the sake of experiment,

94
00:05:19,700 --> 00:05:22,593
‫comment out this code, and then try it again.

95
00:05:24,800 --> 00:05:28,340
‫And this is taking a lot of time, I'm not sure why.

96
00:05:28,340 --> 00:05:32,423
‫So let's do it again, and so you see, 157 milliseconds,

97
00:05:33,280 --> 00:05:37,033
‫and if we now put it back, let's do it again.

98
00:05:39,080 --> 00:05:40,560
‫And again, it takes a lot of time,

99
00:05:40,560 --> 00:05:42,750
‫and so again, I'm doing it twice.

100
00:05:42,750 --> 00:05:45,900
‫But still you see that it took a little bit longer, okay.

101
00:05:45,900 --> 00:05:47,570
‫So not the end of the world,

102
00:05:47,570 --> 00:05:50,450
‫it was just like 100 milliseconds more,

103
00:05:50,450 --> 00:05:52,330
‫but when you reach a certain scale,

104
00:05:52,330 --> 00:05:54,410
‫that might make a difference.

105
00:05:54,410 --> 00:05:57,580
‫So again, I just want you to keep that in mind.

106
00:05:57,580 --> 00:06:01,113
‫Anyway, that's it for this lecture, so let's now move on.

