﻿1
00:00:00,940 --> 00:00:02,720
‫So we learned about Document

2
00:00:02,720 --> 00:00:05,860
‫and query middleware, and now the last middleware

3
00:00:05,860 --> 00:00:09,033
‫that we're gonna talk about is aggregation middleware,

4
00:00:10,560 --> 00:00:12,253
‫and as you can probably guess,

5
00:00:13,168 --> 00:00:14,930
‫aggregation middleware allows us to add hooks

6
00:00:14,930 --> 00:00:18,350
‫before or after an aggregation happens,

7
00:00:18,350 --> 00:00:19,970
‫and so let's now actually continue with

8
00:00:19,970 --> 00:00:23,830
‫our previous example where we did hide the secret tours

9
00:00:23,830 --> 00:00:27,540
‫from the queries, now in an aggregation the secret tours

10
00:00:27,540 --> 00:00:30,270
‫are still being used, right?

11
00:00:30,270 --> 00:00:32,220
‫So let's quickly confirm that actually,

12
00:00:33,840 --> 00:00:37,290
‫for example here in our gets tour stats,

13
00:00:37,290 --> 00:00:39,940
‫so that's where we used the first aggregation

14
00:00:39,940 --> 00:00:42,700
‫and so you see we have four tours here,

15
00:00:42,700 --> 00:00:45,500
‫four in easy, and three in medium

16
00:00:45,500 --> 00:00:49,060
‫and so that makes 11 but we already know

17
00:00:49,060 --> 00:00:52,480
‫that we actually only want 10 tours.

18
00:00:52,480 --> 00:00:55,380
‫So there are 10 tours that are not secret,

19
00:00:55,380 --> 00:00:58,900
‫and one that is secret, and so now we get all these

20
00:00:58,900 --> 00:01:01,880
‫11 tours here and so basically we also want

21
00:01:01,880 --> 00:01:05,000
‫to exclude the secret tour in the aggregation.

22
00:01:05,000 --> 00:01:06,373
‫So how could we do that?

23
00:01:07,430 --> 00:01:09,680
‫Well let's take a look at where our aggregation

24
00:01:09,680 --> 00:01:14,680
‫is actually happening so it's down here in yeah,

25
00:01:14,900 --> 00:01:19,270
‫so in get tour steps, and so what we could do is to here

26
00:01:19,270 --> 00:01:22,640
‫in this match state simply exclude the secret tours

27
00:01:22,640 --> 00:01:24,520
‫that are true right?

28
00:01:24,520 --> 00:01:26,740
‫So that would be quite easy to add here,

29
00:01:26,740 --> 00:01:29,080
‫but then we would have to add the same thing

30
00:01:29,080 --> 00:01:32,080
‫down here in the other aggregation that we have,

31
00:01:32,080 --> 00:01:34,680
‫and if we had even more aggregations we would then

32
00:01:34,680 --> 00:01:37,860
‫have to add that in all of them and that's of course

33
00:01:37,860 --> 00:01:40,560
‫not a good idea, because for example we could forget

34
00:01:40,560 --> 00:01:44,010
‫to do it and it's also just repetitive code

35
00:01:44,010 --> 00:01:48,820
‫and so let's simply exclude it right at the model level,

36
00:01:48,820 --> 00:01:53,280
‫alright so let's add aggregation middleware here

37
00:01:54,470 --> 00:01:55,893
‫starting with a comment.

38
00:01:59,450 --> 00:02:03,970
‫So aggregation middleware and so just like before

39
00:02:05,070 --> 00:02:10,040
‫we have tourSchema.pre so we want this to happen before

40
00:02:10,040 --> 00:02:12,110
‫the aggregation is actually executed

41
00:02:13,060 --> 00:02:16,573
‫and so here we use the aggregate hook.

42
00:02:18,230 --> 00:02:22,047
‫Alright and then our function which again accepts

43
00:02:22,047 --> 00:02:26,332
‫the next function so calling the next middleware.

44
00:02:26,332 --> 00:02:31,332
‫Alright, so let's start by taking a look

45
00:02:32,530 --> 00:02:37,113
‫at this, so remember that up here in query middleware

46
00:02:37,113 --> 00:02:40,283
‫that this object points to the current query.

47
00:02:41,130 --> 00:02:44,590
‫Then here in document middleware the this object

48
00:02:44,590 --> 00:02:46,850
‫points to the current document,

49
00:02:46,850 --> 00:02:50,330
‫and so down here in aggregation middleware

50
00:02:50,330 --> 00:02:53,880
‫this is going to point to the current aggregation object.

51
00:02:53,880 --> 00:02:56,764
‫Okay and so let's take a look at that in the console

52
00:02:56,764 --> 00:03:00,113
‫but before that let me just remove this log here.

53
00:03:01,590 --> 00:03:05,890
‫Alright give it a save, run this aggregation here

54
00:03:08,520 --> 00:03:12,520
‫and that's taking a lot of time and so let's cancel it

55
00:03:13,750 --> 00:03:16,580
‫and go back and that's probably because

56
00:03:16,580 --> 00:03:21,460
‫we're not calling next here, and so like we already know

57
00:03:21,460 --> 00:03:23,580
‫we get stuck in the middleware function

58
00:03:23,580 --> 00:03:26,340
‫and so that's not good.

59
00:03:26,340 --> 00:03:29,063
‫So let's try that again now send,

60
00:03:30,120 --> 00:03:33,490
‫and yeah so now we're back and now let's take a look

61
00:03:33,490 --> 00:03:35,470
‫at the aggregation object.

62
00:03:35,470 --> 00:03:37,700
‫Okay so there's a lot of stuff in here

63
00:03:39,980 --> 00:03:43,800
‫so it's not really interesting here

64
00:03:44,950 --> 00:03:48,330
‫and I cannot really find what I'm trying to show you here

65
00:03:48,330 --> 00:03:53,330
‫and so let's simply add this.pipeline okay because what

66
00:03:53,450 --> 00:03:57,140
‫I really wanted to show you is just the pipeline object.

67
00:03:57,140 --> 00:04:00,620
‫Okay, and actually it's pipeline like this

68
00:04:00,620 --> 00:04:03,053
‫so it's actually a function, a method.

69
00:04:04,970 --> 00:04:06,593
‫So let's clear the console here.

70
00:04:07,820 --> 00:04:12,820
‫Try this again, and so here we get the aggregation pipeline,

71
00:04:14,350 --> 00:04:17,620
‫and so that's simply the array that we passed into

72
00:04:17,620 --> 00:04:19,770
‫the aggregate function before.

73
00:04:19,770 --> 00:04:23,040
‫Right, so let's take a look and so here we have

74
00:04:23,040 --> 00:04:26,400
‫that array, this one not this one.

75
00:04:26,400 --> 00:04:28,200
‫So we have the match to group

76
00:04:28,200 --> 00:04:30,910
‫and then the sort so if we take a look here

77
00:04:30,910 --> 00:04:34,470
‫at the console we have indeed our match with the ratings

78
00:04:34,470 --> 00:04:37,320
‫average we have group and then sort.

79
00:04:37,320 --> 00:04:41,020
‫So that is exactly the pipeline we specified before,

80
00:04:41,020 --> 00:04:44,570
‫and so now in order to filter out the secret tours

81
00:04:44,570 --> 00:04:47,970
‫all we have to do is to add another match stage

82
00:04:47,970 --> 00:04:50,700
‫right at the beginning of this pipeline array,

83
00:04:50,700 --> 00:04:53,713
‫right and so let's do just that.

84
00:04:55,029 --> 00:04:58,040
‫So let's do that here before this log,

85
00:04:58,040 --> 00:05:03,040
‫so this.pipeline and remember that this is an array

86
00:05:03,890 --> 00:05:06,130
‫and how do we add an element at the beginning

87
00:05:06,130 --> 00:05:06,963
‫of an array?

88
00:05:08,262 --> 00:05:11,510
‫We use unshift and so that is a standard JavaScript

89
00:05:11,510 --> 00:05:15,340
‫method for arrays okay, we have also shift to add

90
00:05:15,340 --> 00:05:18,470
‫at the end of the array and unshift at the beginning

91
00:05:18,470 --> 00:05:21,690
‫of the array, and so again what we want to add here now

92
00:05:21,690 --> 00:05:25,490
‫is to add just another stage, so very similar

93
00:05:25,490 --> 00:05:26,803
‫to what we have down here.

94
00:05:29,690 --> 00:05:34,690
‫So match and then secretTour, not equal to true.

95
00:05:40,460 --> 00:05:45,340
‫Give it a save, and so this piece of code here basically

96
00:05:45,340 --> 00:05:48,330
‫is exactly what we have up here right?

97
00:05:48,330 --> 00:05:51,220
‫Basically removing from the output all the documents

98
00:05:51,220 --> 00:05:55,260
‫that have secretTour set to true, right?

99
00:05:55,260 --> 00:06:00,160
‫So if you give it a save now, then let's check this out,

100
00:06:00,160 --> 00:06:03,250
‫and remember we have now 11 tours, four, four,

101
00:06:03,250 --> 00:06:06,780
‫and three so let's check the result afterwards

102
00:06:06,780 --> 00:06:10,340
‫and so now indeed one of the difficult yours is gone

103
00:06:10,340 --> 00:06:13,460
‫so we only now have three difficult tours meaning

104
00:06:13,460 --> 00:06:15,930
‫that we are back to just having 10.

105
00:06:15,930 --> 00:06:20,260
‫Okay and actually we can confirm that our super secret tour

106
00:06:20,260 --> 00:06:24,630
‫was a difficult tour, alright and so that is now gone

107
00:06:24,630 --> 00:06:27,700
‫and so our results are now only about the regular

108
00:06:27,700 --> 00:06:30,800
‫non-secret tours alright?

109
00:06:30,800 --> 00:06:34,670
‫And we can again take a look at our aggregation pipeline now

110
00:06:34,670 --> 00:06:38,640
‫and here you see that now we have these two matched stages.

111
00:06:38,640 --> 00:06:41,150
‫Great and as I mentioned before it's no problem

112
00:06:41,150 --> 00:06:45,050
‫to repeat these stages, we can have as many match stages

113
00:06:45,050 --> 00:06:49,920
‫as we want, okay and that's it for aggregation middleware.

114
00:06:49,920 --> 00:06:53,740
‫I'm not gonna include any post code here for middleware

115
00:06:53,740 --> 00:06:56,290
‫that's not really necessary alright,

116
00:06:56,290 --> 00:06:59,050
‫and I'm also not gonna talk about model middleware here

117
00:06:59,050 --> 00:07:01,570
‫because it's really not that important.

118
00:07:01,570 --> 00:07:04,750
‫Alright, so middleware is some really cool

119
00:07:04,750 --> 00:07:08,180
‫and important stuff that we can add to our models,

120
00:07:08,180 --> 00:07:10,560
‫and there is indeed other cool stuff

121
00:07:10,560 --> 00:07:13,430
‫that we can do with models, for example implementing

122
00:07:13,430 --> 00:07:16,690
‫instance methods which are methods that will be available

123
00:07:16,690 --> 00:07:19,220
‫on ever document after being queried,

124
00:07:19,220 --> 00:07:21,410
‫and that again can be quite handy,

125
00:07:21,410 --> 00:07:23,402
‫and we will do that later

126
00:07:23,402 --> 00:07:25,570
‫in the authentication section as well okay.

127
00:07:25,570 --> 00:07:27,940
‫So I'm gonna leave that for a bit later,

128
00:07:27,940 --> 00:07:30,110
‫now what we are gonna do in this section

129
00:07:30,110 --> 00:07:34,170
‫just to finish is to actually add some validation, alright

130
00:07:34,170 --> 00:07:37,070
‫and so in the next two videos we're gonna talk about that.

