1
00:00:00,150 --> 00:00:06,480
Beautiful, and once we're done setting up aggregation pipeline in Atlas, I want to do the same thing

2
00:00:06,810 --> 00:00:12,730
in our code and in here first, we want to navigate to refugees.

3
00:00:13,170 --> 00:00:16,500
And we're looking for we calculate average rating.

4
00:00:16,770 --> 00:00:17,640
Now, my apologies.

5
00:00:17,640 --> 00:00:24,180
I think I should have went with something more generic because we'll also be looking for number of years,

6
00:00:24,210 --> 00:00:30,420
but hopefully you can forgive me and the way we set everything up is following.

7
00:00:31,200 --> 00:00:33,600
First, I want to go with some kind of value.

8
00:00:33,750 --> 00:00:39,370
In my case, I'm going to call this a result is equal to and this is going to be a synchronous.

9
00:00:39,420 --> 00:00:43,380
So we need to go with a weight when we're looking for our schema.

10
00:00:43,710 --> 00:00:47,130
So we go here with verse and line.

11
00:00:47,400 --> 00:00:49,140
The method name is aggregate.

12
00:00:50,230 --> 00:00:55,360
So syntax is following we go with aggregate, it is a method we invoke.

13
00:00:55,750 --> 00:01:02,890
And just like in our temp, Jess, where essentially we have a our first step, second step.

14
00:01:03,190 --> 00:01:09,700
Sam, we need to do over here where in ways that aggregate, we pass in the right and we'll set up the

15
00:01:09,710 --> 00:01:12,820
steps and you can copy and paste if you want.

16
00:01:12,820 --> 00:01:16,300
In my case, I will set up everything from scratch.

17
00:01:16,690 --> 00:01:22,300
But if you copy and paste, keep in mind that some values you'll have to change here around.

18
00:01:22,780 --> 00:01:24,850
So I'm going to go back here.

19
00:01:24,880 --> 00:01:27,460
I'm going to set up the first step, which is matching.

20
00:01:28,020 --> 00:01:33,340
So go with match and that's the operator, and then I'm going to go with product.

21
00:01:33,520 --> 00:01:34,420
So that's the key.

22
00:01:34,750 --> 00:01:37,000
But in this case, we're not hard coding.

23
00:01:37,480 --> 00:01:40,540
I'm not going to pass in some specific object daddy.

24
00:01:40,870 --> 00:01:46,060
I'll say whatever product the I'm getting in, calculate our trading.

25
00:01:46,450 --> 00:01:47,860
Well, that's the one that I'll use.

26
00:01:48,130 --> 00:01:52,150
Some say product and then I, that's my first stage.

27
00:01:52,570 --> 00:01:56,200
Then we add karma and then where you want to set up another stage.

28
00:01:56,560 --> 00:01:58,290
Now this is the group one, correct?

29
00:01:58,300 --> 00:02:02,710
So we go with group operator and that's the object that we are returning.

30
00:02:03,490 --> 00:02:05,920
And again, I'm going to go with i-D here.

31
00:02:06,430 --> 00:02:11,430
And like Ari previously mentioned, either we go here with product.

32
00:02:12,190 --> 00:02:18,040
So essentially that is just going to get all the reviews in the list that match that product, which

33
00:02:18,040 --> 00:02:19,900
is our list at the end of the day.

34
00:02:20,350 --> 00:02:24,220
So in our example, we can also go with no, that's really up to you.

35
00:02:24,220 --> 00:02:30,280
And I'm not going to repeat the entire set up because I think I already covered up in a previous video

36
00:02:30,460 --> 00:02:37,960
where yes, if you have more complex data, if you have, let's say, some kind of list where you want

37
00:02:37,960 --> 00:02:42,640
to group them by the rating or something like that, then that matters.

38
00:02:42,640 --> 00:02:44,330
Whatever you passing as an idea.

39
00:02:44,350 --> 00:02:45,280
In our case, it doesn't.

40
00:02:45,280 --> 00:02:51,310
Since I want to get the entire list underway and right where we want to go with average rating.

41
00:02:51,340 --> 00:02:53,680
So that's the proper name that I'm getting.

42
00:02:53,890 --> 00:03:01,630
And this one will be equal to an average operator, and I'm going to be looking for my rating.

43
00:03:02,020 --> 00:03:03,070
So rating property.

44
00:03:03,610 --> 00:03:08,240
And also, I want to set up a number of reviews brought in between the videos.

45
00:03:08,260 --> 00:03:10,150
I noticed that there is a tiny bug.

46
00:03:10,510 --> 00:03:13,390
It's not no off line here.

47
00:03:13,390 --> 00:03:14,230
I have no.

48
00:03:15,040 --> 00:03:20,100
So two options, either you change it here or you change it to Nicole and Mike.

49
00:03:20,260 --> 00:03:25,690
I think I'm just going to stick with this one just so I have less things to worry about.

50
00:03:26,020 --> 00:03:27,640
So back in the review.

51
00:03:28,550 --> 00:03:29,750
Let's set up average rating.

52
00:03:29,780 --> 00:03:36,290
That's number one, comma, the number of reviews that is equal to an object, remember some operator

53
00:03:36,590 --> 00:03:38,360
and we showed equal to one.

54
00:03:39,270 --> 00:03:46,350
And what you'll notice once you start deleting, updating or just creating reviews that this value will

55
00:03:46,350 --> 00:03:49,560
be changing, so we go here and we'll go with log.

56
00:03:50,490 --> 00:03:52,800
And I want to see the result.

57
00:03:53,860 --> 00:04:01,480
Right, and we want to go back to the postmen and there, since I'm logged in to Susan, I believe I'm

58
00:04:01,480 --> 00:04:04,510
going to update my review, so let me get all the reviews.

59
00:04:05,790 --> 00:04:13,800
I'll say over here that this is done by and let me take the ad. I mean, the review and then in here,

60
00:04:14,160 --> 00:04:17,130
by the way, I should have went with some different number.

61
00:04:17,490 --> 00:04:23,130
So I go with three and not if we take a look at our code, which you'll notice in the council.

62
00:04:23,130 --> 00:04:23,760
Is this.

63
00:04:23,790 --> 00:04:24,210
All right.

64
00:04:24,990 --> 00:04:31,680
So in the race, since I'm getting all the list items, since I'm grouping all the list items, I have

65
00:04:31,680 --> 00:04:32,640
only one object.

66
00:04:32,910 --> 00:04:40,230
So of course, if you have a more complex grouping, you have multiple objects and you can clearly see

67
00:04:40,230 --> 00:04:41,870
that in Mongo DB docks.

68
00:04:42,300 --> 00:04:44,970
That's why I'm not going to spend any more time on that.

69
00:04:45,330 --> 00:04:49,860
And that in here I have an average rating as well as a number of reviews.

70
00:04:50,070 --> 00:04:51,390
So what's the next step?

71
00:04:51,900 --> 00:04:55,560
Well, the next step is to update the product.

72
00:04:56,470 --> 00:04:58,030
Whose idea is matching this product?

73
00:04:59,110 --> 00:05:06,730
So once I get back my object, I simply want to take these two properties and I want to update of the

74
00:05:06,730 --> 00:05:08,900
product how we can update the product.

75
00:05:08,920 --> 00:05:15,340
Well, we can go with the that model product and then we have access to find one and update.

76
00:05:15,820 --> 00:05:22,660
Now I'm not going to showcase this with post-money yet because I think it's going to take more time.

77
00:05:22,660 --> 00:05:29,740
But we also need to keep in mind that at some point, if we remove all the reviews associated with the

78
00:05:29,740 --> 00:05:32,800
product refresh, Ray will be just empty.

79
00:05:33,640 --> 00:05:34,600
There's going to be nothing there.

80
00:05:34,810 --> 00:05:41,350
Yes, we'll set up the functionality where we update that, but we also need to check whether this object

81
00:05:41,350 --> 00:05:42,520
actually exists.

82
00:05:42,910 --> 00:05:48,430
Therefore, we'll use Chaining Optional Training, which is available in JavaScript, but there's other

83
00:05:48,430 --> 00:05:49,240
methods as well.

84
00:05:49,780 --> 00:05:51,790
So let's go here with a try catch.

85
00:05:51,930 --> 00:05:55,810
First, try catch and in the try block I want to go with.

86
00:05:56,380 --> 00:05:57,220
It is a synchronous.

87
00:05:57,230 --> 00:05:59,020
I'm looking for that model.

88
00:05:59,350 --> 00:06:00,860
And in here, what model do I want?

89
00:06:00,880 --> 00:06:02,350
Well, I want product, correct?

90
00:06:02,350 --> 00:06:10,390
So we'll go here with product when we go find one and update where you know this method.

91
00:06:11,110 --> 00:06:20,200
First thing we pass in is the product ID so we can go here with on the score I.D. and then equals to

92
00:06:20,200 --> 00:06:21,910
a product I.D. Correct?

93
00:06:22,320 --> 00:06:24,160
Then what do we want to update?

94
00:06:24,160 --> 00:06:25,730
Well on a product?

95
00:06:25,750 --> 00:06:27,040
What are the proper names?

96
00:06:27,700 --> 00:06:30,340
Average rating and number of reviews, correct?

97
00:06:30,880 --> 00:06:31,750
Average rating.

98
00:06:32,200 --> 00:06:33,800
And that is equal to what?

99
00:06:33,820 --> 00:06:36,490
Well, now I have my result.

100
00:06:37,000 --> 00:06:37,990
Correct my right.

101
00:06:38,260 --> 00:06:39,670
It's right over here.

102
00:06:40,330 --> 00:06:48,280
But we also need to keep in mind that we'll have to use some kind of math around operation.

103
00:06:48,610 --> 00:06:50,640
So, Mike, I'm just going to go with Matthew.

104
00:06:50,890 --> 00:06:56,860
So same math seal is equal to and nine percent result.

105
00:06:57,680 --> 00:07:00,260
I'm looking for the first item for my object.

106
00:07:00,440 --> 00:07:04,700
But again, there's going to be cases where there are no reviews.

107
00:07:04,790 --> 00:07:08,090
Let's say we remove all the reviews for that one specific product.

108
00:07:08,450 --> 00:07:09,980
Therefore, we'll add here.

109
00:07:10,340 --> 00:07:10,970
Question mark.

110
00:07:11,330 --> 00:07:11,800
And then.

111
00:07:12,290 --> 00:07:14,480
And if you're not familiar, this is optional training.

112
00:07:14,810 --> 00:07:19,850
And if you're interested, I have the video, the entire video or in my YouTube channel decoding at

113
00:07:19,850 --> 00:07:20,030
it.

114
00:07:20,360 --> 00:07:25,730
And more specifically, you're looking for JavaScript Nugget series and there have optional training

115
00:07:25,730 --> 00:07:25,960
one.

116
00:07:26,420 --> 00:07:33,260
And then I'm looking for average rating if it doesn't exist and I simply want to go with zero mike show

117
00:07:33,620 --> 00:07:42,680
and learn what we want to do is copy this and we're looking for not average rating setting number of

118
00:07:42,680 --> 00:07:45,050
years of reviews.

119
00:07:45,470 --> 00:07:47,470
That's the property name on the object again.

120
00:07:47,480 --> 00:07:49,820
We'll go with math that ceiling mount.

121
00:07:49,820 --> 00:07:53,090
In this case, though, Richard don't need to use the math suing.

122
00:07:54,020 --> 00:07:57,380
We'll simply say a result a number of years.

123
00:07:58,100 --> 00:08:01,430
If the property exists, then awesome.

124
00:08:01,790 --> 00:08:07,030
If it doesn't, and we'll just set it equal to A0, we save it.

125
00:08:07,040 --> 00:08:14,780
And now let's test it out where I'm going to go to my post, man, and I'm gonna set it back to one.

126
00:08:15,140 --> 00:08:16,130
Let's send it.

127
00:08:16,490 --> 00:08:21,980
So we're sent back and not sure whether we're still console logging, but we should.

128
00:08:22,100 --> 00:08:25,580
So let me go back here and first in a catch.

129
00:08:25,640 --> 00:08:28,730
Let's just add log and error.

130
00:08:30,300 --> 00:08:35,429
And then above, try and catch, let's log the result, because I want to showcase what is going to

131
00:08:35,429 --> 00:08:40,900
be a result if we are all there viewers and now it's update or I'm sorry.

132
00:08:41,010 --> 00:08:42,270
Now let's check it out.

133
00:08:42,330 --> 00:08:48,660
The get off product and you'll notice that for this product, what's really cool now, average rating

134
00:08:48,660 --> 00:08:50,850
is three because we have five in one.

135
00:08:51,270 --> 00:08:52,830
And what about number of years?

136
00:08:53,280 --> 00:08:54,170
It is one.

137
00:08:54,180 --> 00:09:02,040
So if I log in as John again and I remove all of them, you'll notice some interesting changes where

138
00:09:02,220 --> 00:09:03,690
I'll get all my reviews.

139
00:09:04,090 --> 00:09:05,370
OK, that's a start.

140
00:09:06,160 --> 00:09:10,480
And first, I want to remove John's review, which is five.

141
00:09:10,870 --> 00:09:14,350
I send it, and then again, if I take a look at all the products.

142
00:09:14,740 --> 00:09:15,850
Check it out.

143
00:09:16,240 --> 00:09:17,860
Now I have an average rating one.

144
00:09:18,640 --> 00:09:19,840
That's what we're calculating.

145
00:09:19,840 --> 00:09:20,800
And how many reviews?

146
00:09:21,190 --> 00:09:21,880
Only one.

147
00:09:22,270 --> 00:09:29,380
So if I remove the last one, the Citizens Review notice that everything is going to go back to zero.

148
00:09:29,740 --> 00:09:30,970
So the literature review.

149
00:09:32,160 --> 00:09:39,120
Copy and paste and everything should be back to zero or get all products.

150
00:09:40,500 --> 00:09:45,090
And what do you know, your average rating and number of reviews is zero.

151
00:09:45,470 --> 00:09:49,890
So that's how we can set up the aggregation pipeline in the code.

152
00:09:50,250 --> 00:09:56,400
So if you want, you can utilize some of the code snippets that we got back from Atlas.

153
00:09:56,640 --> 00:09:57,900
That's definitely an option.

154
00:09:58,290 --> 00:10:01,000
But this is essentially how you would Typekit out.

155
00:10:01,020 --> 00:10:04,650
And then lastly, I just want to mention that result is an empty array.

156
00:10:04,980 --> 00:10:11,680
Therefore, we use this option with training where if you will just say, Hey, get me result zero and

157
00:10:11,680 --> 00:10:19,290
then average rating, you get back in error because if is empty, ran JavaScript is going to complain

158
00:10:19,560 --> 00:10:28,320
how I can get the average rating on something that doesn't exist in the array since there is not a first

159
00:10:28,320 --> 00:10:31,170
item in the array, hopefully everything was clear.

160
00:10:31,170 --> 00:10:37,020
I'll remove this console lock for result and now I can move on to setting up our orders.

