﻿1
00:00:01,170 --> 00:00:02,830
‫So we embedded users

2
00:00:02,830 --> 00:00:05,060
‫into tours in the last video,

3
00:00:05,060 --> 00:00:06,870
‫and also talked about the drawbacks

4
00:00:06,870 --> 00:00:10,010
‫of that approach in our specific situation.

5
00:00:10,010 --> 00:00:13,010
‫And so in this video let's actually connect tours

6
00:00:13,010 --> 00:00:17,443
‫and users not by embedding but instead by a reference.

7
00:00:19,050 --> 00:00:19,883
‫And so to start, let's actually get rid

8
00:00:19,883 --> 00:00:24,883
‫of the code we wrote in the last lecture.

9
00:00:24,920 --> 00:00:26,350
‫Okay, so this one here,

10
00:00:26,350 --> 00:00:29,533
‫responsible for performing the embedding basically.

11
00:00:30,650 --> 00:00:33,800
‫And now let's go here to our guides field.

12
00:00:33,800 --> 00:00:34,670
‫Okay?

13
00:00:34,670 --> 00:00:36,570
‫So this time in this video,

14
00:00:36,570 --> 00:00:40,860
‫the idea is that tours and users will always remain

15
00:00:40,860 --> 00:00:43,760
‫completely separate entities in our database.

16
00:00:43,760 --> 00:00:44,593
‫Okay?

17
00:00:44,593 --> 00:00:47,130
‫So all we save on a certain tour document

18
00:00:47,130 --> 00:00:50,290
‫is the IDs of the users that are the tour guides

19
00:00:50,290 --> 00:00:52,000
‫for that specific tour.

20
00:00:52,000 --> 00:00:53,900
‫Then when we query the tour,

21
00:00:53,900 --> 00:00:57,170
‫we want to automatically get access to the tour guides.

22
00:00:57,170 --> 00:00:59,450
‫But again, without them being actually

23
00:00:59,450 --> 00:01:02,060
‫saved on the tour document itself.

24
00:01:02,060 --> 00:01:04,320
‫And that exactly is referencing.

25
00:01:04,320 --> 00:01:08,150
‫So just as we talked about by the beginning of this section.

26
00:01:08,150 --> 00:01:08,983
‫All right?

27
00:01:08,983 --> 00:01:10,020
‫And so let me now show you

28
00:01:10,020 --> 00:01:13,419
‫how we can implement referencing using Mongoose.

29
00:01:13,419 --> 00:01:14,450
‫Okay?

30
00:01:14,450 --> 00:01:18,545
‫So, here in the guides we will now want to specify an array.

31
00:01:18,545 --> 00:01:19,610
‫Okay?

32
00:01:19,610 --> 00:01:23,000
‫So just like we did before with the locations.

33
00:01:23,000 --> 00:01:24,510
‫And so that then again means

34
00:01:24,510 --> 00:01:26,930
‫that these will be some sub-documents.

35
00:01:26,930 --> 00:01:28,770
‫So embedded documents.

36
00:01:28,770 --> 00:01:29,603
‫All right?

37
00:01:30,480 --> 00:01:32,360
‫And now the type is going to be

38
00:01:32,360 --> 00:01:35,470
‫a new type that we never saw before.

39
00:01:35,470 --> 00:01:40,470
‫And that is mongoose.Schema.objectId.

40
00:01:44,715 --> 00:01:45,599
‫Okay?

41
00:01:45,599 --> 00:01:47,874
‫And what this means is that we expect

42
00:01:47,874 --> 00:01:49,780
‫a type of each of the elements

43
00:01:49,780 --> 00:01:53,220
‫in the guides array to be a MongoDB ID.

44
00:01:53,220 --> 00:01:54,053
‫All right?

45
00:01:54,053 --> 00:01:57,220
‫And here actually it needs to be a capital O,

46
00:01:57,220 --> 00:01:59,520
‫okay, otherwise it's not gonna work,

47
00:01:59,520 --> 00:02:03,880
‫and also all of this here needs to be inside of an object,

48
00:02:03,880 --> 00:02:06,880
‫just like any other schema type definition.

49
00:02:06,880 --> 00:02:09,403
‫Okay, because that's all this really is.

50
00:02:11,140 --> 00:02:16,140
‫Okay, so the type is of this MongoDB ID, basically,

51
00:02:16,670 --> 00:02:20,303
‫and then we also need to now specify the reference.

52
00:02:21,210 --> 00:02:24,020
‫And this is where the magic happens behind the scenes,

53
00:02:24,020 --> 00:02:28,630
‫because here, now we say that the reference should be user.

54
00:02:28,630 --> 00:02:32,140
‫Okay, and so this really is how we establish references

55
00:02:32,140 --> 00:02:35,170
‫between different data sets in Mongoose.

56
00:02:35,170 --> 00:02:37,540
‫And for this we actually do not even need

57
00:02:37,540 --> 00:02:42,060
‫to have the user to be imported into this document.

58
00:02:42,060 --> 00:02:44,910
‫So we actually had that here, but we don't even need it.

59
00:02:44,910 --> 00:02:46,970
‫We had that here for the previous lecture,

60
00:02:46,970 --> 00:02:48,620
‫but we can comment it out,

61
00:02:48,620 --> 00:02:52,455
‫because what we just did here is still gonna work.

62
00:02:52,455 --> 00:02:53,950
‫Okay?

63
00:02:53,950 --> 00:02:57,420
‫So, let's now go ahead and create a new tour,

64
00:02:57,420 --> 00:03:00,370
‫and actually I'm going to do it just the way we did before.

65
00:03:01,440 --> 00:03:03,973
‫So let's just go ahead and delete this one.

66
00:03:05,400 --> 00:03:06,833
‫Where is the ID?

67
00:03:08,931 --> 00:03:10,560
‫Here we go.

68
00:03:10,560 --> 00:03:11,933
‫So delete tour.

69
00:03:14,760 --> 00:03:17,113
‫So we can use our API for that already.

70
00:03:19,700 --> 00:03:23,600
‫All right, and now we create a new tour.

71
00:03:23,600 --> 00:03:24,930
‫And so just like before,

72
00:03:24,930 --> 00:03:28,950
‫all we pass into the guides is an array of the IDs.

73
00:03:28,950 --> 00:03:29,783
‫All right?

74
00:03:29,783 --> 00:03:31,226
‫But this time we actually specified

75
00:03:31,226 --> 00:03:35,010
‫that an object ID is exactly what we expect.

76
00:03:35,010 --> 00:03:35,843
‫Right?

77
00:03:35,843 --> 00:03:39,420
‫So, this here is of the type object ID.

78
00:03:39,420 --> 00:03:42,957
‫But behind the scenes, it's also referenced to the user.

79
00:03:42,957 --> 00:03:44,200
‫All right?

80
00:03:44,200 --> 00:03:47,300
‫So, when we now create this tour here,

81
00:03:47,300 --> 00:03:50,090
‫it will actually only contain these IDs,

82
00:03:50,090 --> 00:03:53,640
‫and not the user corresponding to the IDs.

83
00:03:53,640 --> 00:03:55,090
‫So let's take a look at that.

84
00:03:56,500 --> 00:03:59,040
‫And so yeah, here we go.

85
00:03:59,040 --> 00:04:01,720
‫So this is exactly the data that we put in.

86
00:04:01,720 --> 00:04:04,430
‫Just the IDs of the tour guides.

87
00:04:04,430 --> 00:04:07,010
‫Okay, and that's exactly what we were expecting.

88
00:04:07,010 --> 00:04:09,410
‫Because for now, all we really have

89
00:04:09,410 --> 00:04:11,790
‫is the references inside Mongoose.

90
00:04:11,790 --> 00:04:14,430
‫And we will then take care of actually getting the data

91
00:04:14,430 --> 00:04:17,730
‫to show up in our output in the next video.

92
00:04:17,730 --> 00:04:20,330
‫Okay, because we will do that in two different ways.

93
00:04:21,210 --> 00:04:24,323
‫Let's also take a look here at Compass really quick,

94
00:04:25,930 --> 00:04:30,930
‫just to see that as well where is that, yeah, here.

95
00:04:30,960 --> 00:04:33,680
‫So you see object ID actually,

96
00:04:33,680 --> 00:04:36,480
‫and then of course, the IDs that we passed in.

97
00:04:36,480 --> 00:04:39,960
‫So just as we expected with tours and users,

98
00:04:39,960 --> 00:04:42,620
‫still two completely different entities.

99
00:04:42,620 --> 00:04:44,860
‫And as I said before, in the next video,

100
00:04:44,860 --> 00:04:47,290
‫we will then take care of actually displaying

101
00:04:47,290 --> 00:04:49,030
‫the user data in the output,

102
00:04:49,030 --> 00:04:51,533
‫using a process called populating.

