1
00:00:00,360 --> 00:00:07,350
Marsh, and once we're familiar with Populate, let's also try to apply to our product.

2
00:00:08,610 --> 00:00:09,540
What am I talking about?

3
00:00:09,570 --> 00:00:17,610
Well, when we are getting single product or if you want, when we're getting all the products, even

4
00:00:17,610 --> 00:00:21,690
though most likely you'll use this one when you're getting a single product.

5
00:00:22,140 --> 00:00:31,980
Not only I want to get the data, but I also want to get all the reviews that are associated with the

6
00:00:31,980 --> 00:00:32,400
product.

7
00:00:33,180 --> 00:00:40,320
Now the problem is that you cannot just simply go back to your product controller and then when you're

8
00:00:40,320 --> 00:00:41,760
looking for a single product.

9
00:00:42,360 --> 00:00:47,850
Go here with dot and then populate and then say Reviews.

10
00:00:47,850 --> 00:00:48,300
Why?

11
00:00:48,660 --> 00:00:51,450
Well, because such connection does not exist.

12
00:00:51,930 --> 00:00:56,490
We haven't connected our product to reviews.

13
00:00:57,000 --> 00:01:01,410
And in order to accomplish our task, we'll have to use mongoose rituals.

14
00:01:01,890 --> 00:01:06,840
And you can think of virtually as properties that do not persist.

15
00:01:07,850 --> 00:01:09,710
Or are stored in the database.

16
00:01:10,040 --> 00:01:11,810
They only exist logically.

17
00:01:12,350 --> 00:01:16,730
So essentially, we create them on the fly when we want to compute something.

18
00:01:17,260 --> 00:01:20,570
And in order to set it up, we'll have to do a little bit more work.

19
00:01:20,600 --> 00:01:21,080
Why?

20
00:01:21,590 --> 00:01:28,220
Well, because if we take a look at our product model, well, there is no reference of review, is

21
00:01:28,220 --> 00:01:28,490
there?

22
00:01:29,030 --> 00:01:38,180
So unlike the review where we were already referencing product and user, yes, we can accept using

23
00:01:38,180 --> 00:01:38,690
populate.

24
00:01:39,260 --> 00:01:48,290
But in the case of product, we have to do some additional logic and that additional logic is going

25
00:01:48,290 --> 00:01:50,960
to look something like this or first.

26
00:01:51,910 --> 00:01:53,740
Where we have timestamps.

27
00:01:54,160 --> 00:01:55,530
You want to go with Comma.

28
00:01:56,110 --> 00:01:58,390
We're want to go it to you, Jason.

29
00:01:58,720 --> 00:02:03,940
And we want to include virtual show a reviews.

30
00:02:04,090 --> 00:02:11,200
We'll add it as a virtual property and for everything to work, we need to go here with this object

31
00:02:11,530 --> 00:02:13,810
and write off their timestamps.

32
00:02:13,840 --> 00:02:19,450
Now, if you don't have the time stamps, of course, then you'll just pass a year to Jason when we

33
00:02:19,450 --> 00:02:22,030
want to go with virtual URLs and true.

34
00:02:22,840 --> 00:02:25,160
And we want to set the same thing to object.

35
00:02:25,160 --> 00:02:26,170
So to object.

36
00:02:26,920 --> 00:02:29,470
And then we'll go with a virtual true.

37
00:02:29,890 --> 00:02:34,240
So we set up this model to accept virtual.

38
00:02:34,510 --> 00:02:36,310
That's step number one one.

39
00:02:36,850 --> 00:02:39,640
I want to create that virtual property.

40
00:02:40,390 --> 00:02:43,280
So the one that I just set up in the controller.

41
00:02:43,330 --> 00:02:46,930
So if your name does differently, then please use that name.

42
00:02:47,080 --> 00:02:55,210
But in my case or in get single product, I said that I want to get the product and I also want to populate

43
00:02:55,690 --> 00:03:01,330
the reviews a property that at the moment does not exist on my model.

44
00:03:01,360 --> 00:03:02,470
How we can set it up.

45
00:03:02,560 --> 00:03:04,390
Well, we go with schema name.

46
00:03:04,780 --> 00:03:08,230
So in this case, of course, product scheme, not virtual.

47
00:03:08,770 --> 00:03:10,300
And then we go with the name.

48
00:03:10,720 --> 00:03:14,410
So in my case, I'm going to order reviews because that's what I used in the controller.

49
00:03:14,980 --> 00:03:17,680
And then we want to set up that connection.

50
00:03:18,190 --> 00:03:20,650
First, we want to set up a reference.

51
00:03:20,770 --> 00:03:21,880
So we go with rep.

52
00:03:22,450 --> 00:03:24,550
And what is going to be the model?

53
00:03:24,580 --> 00:03:25,000
Correct.

54
00:03:25,210 --> 00:03:29,650
So in our case, we go wherever you want and we want to go with a local field.

55
00:03:30,070 --> 00:03:32,830
So we're looking for some kind of connection between the two.

56
00:03:33,460 --> 00:03:40,590
And if I take a look at the review, I can see that there is a product property, which is the eddy

57
00:03:40,960 --> 00:03:43,210
correct now in the product.

58
00:03:43,990 --> 00:03:45,790
What is that a property look like?

59
00:03:46,210 --> 00:03:48,790
The local field is on the score.

60
00:03:49,570 --> 00:03:49,950
Correct.

61
00:03:49,960 --> 00:03:54,640
So we go here, we underscore Eddy, and then we want to pass the foreign field.

62
00:03:55,120 --> 00:03:57,730
So what is going to be the field in the review?

63
00:03:58,750 --> 00:04:02,280
And that field will be product when we add karma.

64
00:04:02,680 --> 00:04:10,270
And since I want to get the list, we want to go with just one and false and then in a second, I'll

65
00:04:10,270 --> 00:04:13,180
show you one more option that we have.

66
00:04:13,840 --> 00:04:15,190
So now let's save it.

67
00:04:16,279 --> 00:04:23,720
And if I go back, I should be able to see all the reviews for that one specific product because for

68
00:04:23,720 --> 00:04:28,400
the time being, I think we have been adding all the time reviews for the same product.

69
00:04:29,260 --> 00:04:35,500
So let's get all the reviews first, and then this is going to be the are you ready?

70
00:04:35,920 --> 00:04:37,870
What I'm looking for is this product.

71
00:04:38,750 --> 00:04:42,440
And if I go to your get single product here.

72
00:04:43,330 --> 00:04:47,190
I can send it, and I should be able to see their views.

73
00:04:47,600 --> 00:04:54,670
So notice reviews, property now and here I have better your I.D. and arrest of the cool stuff.

74
00:04:55,270 --> 00:05:01,120
Now what's also really nifty that we can match specific documents?

75
00:05:01,720 --> 00:05:10,160
So for example, I want to get only the reviews where the rating is one how I'm going to do that.

76
00:05:10,180 --> 00:05:13,930
Well, first, I guess I'll have to update the review, correct?

77
00:05:14,560 --> 00:05:16,330
So let's go back here.

78
00:05:17,190 --> 00:05:21,220
And yeah, let me let me get all the reviews again.

79
00:05:21,370 --> 00:05:23,080
I believe I'm logged in as beta.

80
00:05:23,990 --> 00:05:25,650
So what I user.

81
00:05:25,910 --> 00:05:29,170
We're going to go and show current one, I am Peter.

82
00:05:29,780 --> 00:05:33,080
So let me get all their views and let's see.

83
00:05:33,890 --> 00:05:36,140
So where is the beta one?

84
00:05:37,360 --> 00:05:41,460
I think somewhere it says by Peter, give this one.

85
00:05:41,560 --> 00:05:43,090
So let me grab this one.

86
00:05:44,160 --> 00:05:45,480
And updated.

87
00:05:47,430 --> 00:05:47,880
Update.

88
00:05:48,710 --> 00:05:50,630
And the rating now will be five.

89
00:05:51,170 --> 00:05:51,860
OK, awesome.

90
00:05:52,640 --> 00:05:55,250
I'm fine where I'm getting that single product.

91
00:05:55,760 --> 00:06:04,430
I can also go back to my text editor and we can set up a match where I say, Get me only the document

92
00:06:04,430 --> 00:06:07,280
where rating is equal to five.

93
00:06:07,850 --> 00:06:10,880
So once we go back, we get the single product.

94
00:06:11,240 --> 00:06:17,300
Now we only have one review, one that has that rating of five.

95
00:06:17,660 --> 00:06:19,550
Hopefully, very clear.

96
00:06:19,940 --> 00:06:22,190
So in my case, I'll remove this one.

97
00:06:22,640 --> 00:06:30,140
But before I let you go, one very, very important thing that I want to mention is the fact that this

98
00:06:30,140 --> 00:06:32,390
is a virtual property.

99
00:06:33,170 --> 00:06:41,870
So what that means, unlike when we are going for our model, the product one, you cannot query it.

100
00:06:42,470 --> 00:06:46,340
So you cannot say, Hey, get me this specific review or that one?

101
00:06:46,340 --> 00:06:46,580
No.

102
00:06:46,880 --> 00:06:48,620
You'll just get all the reviews.

103
00:06:48,920 --> 00:06:55,700
And therefore, in the next video, I'll showcase the alternative approach what we can take so we can

104
00:06:55,700 --> 00:07:02,540
set up actual queries on the reviews that are associated with specific product.

