1
00:00:00,390 --> 00:00:00,840
Beautiful.

2
00:00:01,200 --> 00:00:04,050
And once we have set our database back to one product.

3
00:00:04,410 --> 00:00:11,340
Now let's see how we can set up aggregation pipeline and in the process, get average rating as well

4
00:00:11,340 --> 00:00:15,720
as count for all reviews for that one specific product.

5
00:00:16,500 --> 00:00:17,970
There will be two videos on this.

6
00:00:17,970 --> 00:00:21,030
First, I'll show you the gooey option and atlas.

7
00:00:21,270 --> 00:00:25,020
So essentially how we can do that in graphical interface.

8
00:00:25,440 --> 00:00:28,560
Therefore, if you're not logged in, please do so.

9
00:00:28,980 --> 00:00:36,360
And second will repeat the most steps in the code since we also want to interact with our product model

10
00:00:36,360 --> 00:00:40,740
because remember, both of those properties are actually on a product model.

11
00:00:41,160 --> 00:00:44,730
We'll just use reviews model to calculate that.

12
00:00:44,970 --> 00:00:50,310
So every time we update the reviews, we'll send off some code to a product.

13
00:00:50,820 --> 00:00:56,790
Now, when it comes to aggregation, it's pretty complex MongoDB topic.

14
00:00:57,180 --> 00:01:04,769
So if you want to find more info on some operators that I'm going to showcase, or if you want to find

15
00:01:04,769 --> 00:01:11,760
out about different setups, please utilize MongoDB docs since in that you can find all the information.

16
00:01:12,180 --> 00:01:20,550
So something that you are looking at the same database, the e-commerce one, you can see that one product.

17
00:01:20,850 --> 00:01:26,190
I have two reviews and I have three users now in order to set up aggregation.

18
00:01:26,670 --> 00:01:32,340
I just need to look for my collection, which in my case again, is going to be reviews and then we'll

19
00:01:32,550 --> 00:01:35,220
communicate with a product line.

20
00:01:35,250 --> 00:01:38,250
We bravely click here on the aggregation.

21
00:01:38,730 --> 00:01:46,350
And when it comes to aggregation pipeline, you need to think of it as steps so we can match all the

22
00:01:46,350 --> 00:01:49,920
product of animal group based on some kind of pattern.

23
00:01:50,220 --> 00:01:52,430
And then optionally, we can sort as well.

24
00:01:52,500 --> 00:01:54,540
Now, in our case, we're not going to add sorting.

25
00:01:54,810 --> 00:02:02,100
We're just looking for two things I want to match all the products where the product data is this one,

26
00:02:02,580 --> 00:02:07,890
which of course at the moment seems silly because that's the only reviews that I have for that one product.

27
00:02:08,280 --> 00:02:14,970
But we need to understand that eventually there's going to be more product and once I match all their

28
00:02:14,970 --> 00:02:17,820
views that one specific product I to run.

29
00:02:18,690 --> 00:02:19,590
What do I want to do?

30
00:02:20,040 --> 00:02:26,250
Well, I want a group, I want to group them and then take a look at the average rating, as well as

31
00:02:26,250 --> 00:02:27,690
how many reviews I have.

32
00:02:28,320 --> 00:02:30,910
So the first step is following where we go here.

33
00:02:31,290 --> 00:02:33,390
I want to look for Match.

34
00:02:33,960 --> 00:02:37,530
So that's the operating name again, if you want to find out more about the operator.

35
00:02:37,860 --> 00:02:39,720
Please utilize MongoDB docs.

36
00:02:40,050 --> 00:02:41,640
We go here with Match.

37
00:02:42,150 --> 00:02:44,130
And this is where I need to say Hey.

38
00:02:45,130 --> 00:02:52,540
I want to get all their views on where the property matters, something now, what is the property that

39
00:02:52,540 --> 00:02:53,290
I'm looking for?

40
00:02:53,710 --> 00:02:55,630
Well, it is product, correct?

41
00:02:55,660 --> 00:02:56,110
Why?

42
00:02:56,410 --> 00:03:02,050
Well, because if I take a look at my code, I can clearly see, but I'm going to be passing in the

43
00:03:02,050 --> 00:03:05,440
product I.D. so we can already put two and two together.

44
00:03:06,040 --> 00:03:13,570
Where since this is dynamic, since I'm getting here, this product that I want to match all the reviews

45
00:03:14,290 --> 00:03:16,720
where the product eddy is the same value.

46
00:03:17,050 --> 00:03:17,410
Correct.

47
00:03:17,920 --> 00:03:25,440
So let's go back here and the name here needs to match exactly because on my scheme, I have the product.

48
00:03:25,450 --> 00:03:28,540
So here we go with product.

49
00:03:28,840 --> 00:03:32,830
And yes, in this case, we'll hard code because we're working in a good way.

50
00:03:32,920 --> 00:03:35,440
But remember, in our code, it's going to be a little bit different.

51
00:03:35,920 --> 00:03:42,820
So now what I want to do is go with object I.D. and then just grab this sucker over here, make sure

52
00:03:42,820 --> 00:03:47,050
that you get the product one and also make sure that the syntax is correct.

53
00:03:47,380 --> 00:03:52,990
If you don't see any reviews, for example, if I remove this one, I won't see any reviews.

54
00:03:53,260 --> 00:03:55,180
That means that there's some kind of bug.

55
00:03:55,660 --> 00:03:58,510
So make sure that you also see two documents.

56
00:03:58,510 --> 00:04:04,780
And again, eventually this is going to be dynamic and eventually we'll have more of used for time being.

57
00:04:04,810 --> 00:04:12,040
All I'm saying is get me all the reviews where product key as this value.

58
00:04:12,580 --> 00:04:13,030
All right.

59
00:04:13,180 --> 00:04:14,080
That's the first step.

60
00:04:14,380 --> 00:04:17,380
So now we matched all documents in the next stage.

61
00:04:17,709 --> 00:04:19,200
Now we want to group them together.

62
00:04:19,209 --> 00:04:23,050
So we go with add stage and we're looking for group.

63
00:04:24,150 --> 00:04:29,610
And notice how right away they spin back some very useful examples.

64
00:04:29,820 --> 00:04:33,810
And keep in mind that this is something that we are constructing.

65
00:04:34,780 --> 00:04:41,590
Meaning in here, I'll create an I.D. property, and I want to set it equal to some kind of expression.

66
00:04:42,010 --> 00:04:51,070
Now to tell you, honestly, since we have both interviews for same product and our data is not that

67
00:04:51,070 --> 00:04:53,680
complex, we can actually pass here.

68
00:04:53,710 --> 00:05:00,070
No, this is the case where Mongol dogs really come in handy, where you can take a look, right?

69
00:05:00,070 --> 00:05:01,540
So group by expression.

70
00:05:01,870 --> 00:05:05,680
Now, in our case, we don't want a group based on anything.

71
00:05:06,160 --> 00:05:09,010
We really have the list and we're pretty much good to go.

72
00:05:09,700 --> 00:05:12,550
There's really no need for us to add anything expression.

73
00:05:12,820 --> 00:05:15,450
Now can you add here product I.D.?

74
00:05:15,850 --> 00:05:17,360
Yes, of course you can, if you want.

75
00:05:17,380 --> 00:05:19,510
But again, it's not going to make any kind of difference.

76
00:05:20,320 --> 00:05:26,530
The difference here is going to be if you have some kind of more specific data, but you want to go

77
00:05:26,540 --> 00:05:26,830
by.

78
00:05:27,280 --> 00:05:35,170
So again, imagine I don't know a reviews and you want to group by the a review rating.

79
00:05:35,590 --> 00:05:41,740
So that's the case where not only you're getting the list price, you want to group them by the rating.

80
00:05:42,040 --> 00:05:43,060
Hopefully, that's clear.

81
00:05:43,060 --> 00:05:47,050
And if not, then please utilize the box and you'll see that again.

82
00:05:47,050 --> 00:05:52,150
In this case, we have two options either I can just pass on your null since I'm looking for the entire

83
00:05:52,150 --> 00:05:59,170
list to begin with, or if you want to be really specific and you can type out the product here, but

84
00:05:59,440 --> 00:06:01,510
it's not going to make much of a difference.

85
00:06:01,960 --> 00:06:07,960
So next, we want to create some fields, correct, and we're looking for two things.

86
00:06:08,530 --> 00:06:10,780
We're looking for the average rating.

87
00:06:11,350 --> 00:06:14,110
And I also want to get the number of reviews.

88
00:06:14,560 --> 00:06:19,750
So first, if I just remove this field name and as a side note, again, this is something that we are

89
00:06:19,750 --> 00:06:20,350
creating.

90
00:06:20,950 --> 00:06:25,720
So I'm saying that there's going to be an I.D. property there now for the time being, the value will

91
00:06:25,720 --> 00:06:26,260
be now.

92
00:06:26,710 --> 00:06:28,660
And there's also going to be some other fields.

93
00:06:28,660 --> 00:06:32,950
So if I remove this moment, this is the only thing that I get back.

94
00:06:33,640 --> 00:06:39,500
Now what I also want is to get average rating, and that will be equal to.

95
00:06:39,880 --> 00:06:46,060
So this index is following where we need to set up here to curlies and then we're looking for another

96
00:06:46,060 --> 00:06:46,690
operator.

97
00:06:46,960 --> 00:06:49,180
And in this case, I'm looking for the average one.

98
00:06:49,870 --> 00:06:55,000
And then I want to set it equal to whatever property I'm looking for.

99
00:06:55,030 --> 00:06:58,270
So I want to get the average on what?

100
00:06:59,100 --> 00:07:04,670
Well, I want to get the average on the rating, correct, so rating is the one that's changing.

101
00:07:04,670 --> 00:07:07,380
In one case, it's five and the other one, it's one.

102
00:07:07,770 --> 00:07:13,260
Now how we can set that average while we go here for the property, but we do need to add this.

103
00:07:14,080 --> 00:07:19,140
I'm sorry to say here a rating and run rate should return.

104
00:07:19,140 --> 00:07:19,510
Three.

105
00:07:19,530 --> 00:07:19,980
Why?

106
00:07:20,090 --> 00:07:21,510
Well, because they have five and one.

107
00:07:22,140 --> 00:07:23,820
You can see it is getting Colquitt.

108
00:07:24,330 --> 00:07:29,420
And then lastly, I want to get the number of reviews again of these properties.

109
00:07:29,430 --> 00:07:33,840
The only reason why I'm naming this way, because that's how we have them in the product.

110
00:07:34,470 --> 00:07:38,940
But remember, whatever we're getting back here, this is what we're setting up ourselves.

111
00:07:39,300 --> 00:07:39,690
Correct.

112
00:07:40,110 --> 00:07:41,790
So and here we just use the operators.

113
00:07:41,790 --> 00:07:46,020
But as far as the keys, I'm setting them up myself.

114
00:07:46,560 --> 00:07:51,990
Let's go here with number of reviews viewers allowed is equal to you.

115
00:07:52,350 --> 00:07:54,080
We'll set up the curlies now.

116
00:07:54,090 --> 00:07:55,260
Use another operator.

117
00:07:55,270 --> 00:07:59,220
We'll go with some index if you want to just some of them up.

118
00:07:59,400 --> 00:08:01,170
If so, that equal to your one.

119
00:08:01,740 --> 00:08:02,670
And now check it out.

120
00:08:03,330 --> 00:08:07,680
I have an average rating a number of years and I get the values.

121
00:08:07,950 --> 00:08:11,610
I fully understand that probably the idea is the most confusing one.

122
00:08:11,910 --> 00:08:19,740
Again, I know I said this before, but the reason why I can add here now is because I already look

123
00:08:19,740 --> 00:08:21,130
for the entire list.

124
00:08:21,180 --> 00:08:28,380
If you're going to have some kind of property that the value is actually changing because keep in mind

125
00:08:28,380 --> 00:08:30,530
that for a product, the value is not changing.

126
00:08:30,540 --> 00:08:35,880
Therefore, I can either go with it now or I can actually set it equal to product.

127
00:08:36,120 --> 00:08:37,120
It doesn't really matter.

128
00:08:37,140 --> 00:08:42,000
Just keep in mind that the syntax would be following where you are looking for a product and then notice

129
00:08:42,000 --> 00:08:42,330
them.

130
00:08:42,690 --> 00:08:49,770
My value did not change because you need to keep in mind that I'm grouping them by that ID value.

131
00:08:49,980 --> 00:08:54,960
And since the add value is exactly the same, therefore the result is exactly the same.

132
00:08:55,410 --> 00:09:02,100
But if you want to group them by something that is changing around, you'll get multiple values.

133
00:09:02,340 --> 00:09:05,520
And again, this is something that you can clearly see in a box.

134
00:09:05,820 --> 00:09:11,940
That's why I don't want to go out of the way and start setting up some kind of wannabe example because

135
00:09:11,940 --> 00:09:17,550
you can clearly read this option in a box and once we have all of this in place.

136
00:09:17,830 --> 00:09:19,020
Now let me go back to now.

137
00:09:19,860 --> 00:09:25,260
So I'm going to go here with, no, I run if we want to get the code.

138
00:09:25,650 --> 00:09:33,840
Essentially, if we want to just get the code snippet that we can use in our code base and we can simply

139
00:09:33,840 --> 00:09:35,340
go to export pipeline.

140
00:09:36,180 --> 00:09:39,210
We're not looking for a python for the node.

141
00:09:39,840 --> 00:09:44,430
And then let's also add both of these like so.

142
00:09:45,240 --> 00:09:52,320
And what you're looking for here is not the MongoDB, not the driver and all that what you want or here

143
00:09:52,650 --> 00:09:54,480
is this one, the aggregation.

144
00:09:54,810 --> 00:09:59,970
Now, in my case, like I said, we'll start everything from scratch regardless because there's more

145
00:09:59,970 --> 00:10:01,290
functionality that you run on.

146
00:10:01,800 --> 00:10:06,660
So I suggest pretty much just either taking this whole thing.

147
00:10:07,080 --> 00:10:14,250
Maybe let's do it this way and then let's navigate back to our project and let's set up a temp file

148
00:10:14,250 --> 00:10:15,720
where we have the models.

149
00:10:16,200 --> 00:10:21,660
So on here, I would just say temp and jazz, and let's copy and paste again.

150
00:10:21,660 --> 00:10:23,410
We're not going to use this functionality.

151
00:10:23,430 --> 00:10:25,920
We're literally just looking for disarray.

152
00:10:26,100 --> 00:10:28,170
That's the only thing we want over here.

153
00:10:28,560 --> 00:10:30,360
So for the time being, we'll just save it.

154
00:10:30,630 --> 00:10:36,750
So that's how we can set up aggregation pipeline in the glory, in the graphical interface.

