1 00:00:02,260 --> 00:00:11,260 Let's look into our friends in their raw form again and let's now say we want to transform exam scores 2 00:00:12,040 --> 00:00:17,620 to be an array where we only see scores higher than 60. 3 00:00:17,770 --> 00:00:22,840 Now this can also be done in the projection phase because I want to transform every single french record 4 00:00:22,840 --> 00:00:24,380 I don't want to group by anything, 5 00:00:24,460 --> 00:00:32,660 I just want to transform the array in there. And to achieve this, I'll add exam scores again, you could name 6 00:00:32,670 --> 00:00:34,190 this differently though, 7 00:00:34,190 --> 00:00:35,380 this is up to you 8 00:00:35,740 --> 00:00:40,570 and in there, you can now use the special filter operator. 9 00:00:40,600 --> 00:00:48,850 Now how does filter work? Filter allows you to filter out certain elements in an array and only return 10 00:00:48,850 --> 00:00:51,780 these that fulfill a certain condition, 11 00:00:52,000 --> 00:00:56,510 so here I would want to filter for the score being greater than 60. 12 00:00:56,530 --> 00:00:57,910 Now how does this work? 13 00:00:57,910 --> 00:01:05,550 First of all you need to add an input here and that is the array which you want to filter, 14 00:01:05,590 --> 00:01:10,480 in our case we could say it's obvious that I want to filter an exam scores because I will store the 15 00:01:10,480 --> 00:01:12,720 result in a field named exam scores 16 00:01:12,730 --> 00:01:16,210 but keep in mind, you can name this field however you want. 17 00:01:16,210 --> 00:01:22,320 So therefore filter needs to know where is the input array and the input array is exam scores with a dollar sign 18 00:01:22,330 --> 00:01:25,310 because I'm pointing at a field in my document. 19 00:01:25,570 --> 00:01:27,820 Then we assign a temporary name, 20 00:01:27,820 --> 00:01:32,590 this is a so-called local variable which will be used inside our filter expression which we'll write 21 00:01:32,590 --> 00:01:33,640 in a second, 22 00:01:33,640 --> 00:01:35,500 you can name this however you want, 23 00:01:35,500 --> 00:01:37,600 I'll name it sc to not repeat 24 00:01:37,630 --> 00:01:38,740 scores all the time 25 00:01:38,770 --> 00:01:42,940 but you can name this however you want. The last element we need 26 00:01:42,950 --> 00:01:48,460 or the last field is the condition, cond. 27 00:01:48,500 --> 00:01:54,590 Now the conditions here can take a bunch of expression operators to which you also find a link in 28 00:01:54,590 --> 00:01:59,630 the last lecture of this module and I'll just bring this into a new line to make it easier to read. The 29 00:01:59,630 --> 00:02:05,570 expression operator I want to use here is greater than, now greater than here works a bit different than 30 00:02:05,570 --> 00:02:11,390 when we used it in match or find. Here greater than simply takes an array of values it should compare 31 00:02:12,140 --> 00:02:20,150 and this makes sense because we are now in the context of another operator, so we can't compare exam 32 00:02:20,150 --> 00:02:22,950 scores the whole array or anything like that, 33 00:02:23,240 --> 00:02:31,790 instead here what I want to compare inside of my filter expression is of course the value of this temporary 34 00:02:31,790 --> 00:02:36,020 variable which will refer to the different values in my exam scores, 35 00:02:36,020 --> 00:02:42,550 so you can imagine this being a function that gets executed for every exam score in my exam scores array 36 00:02:42,830 --> 00:02:47,780 and then every value will temporarily be stored in here so that we can compare it. 37 00:02:47,840 --> 00:02:56,420 So here in greater than, I will check if my sc variable is greater than 60, 38 00:02:56,450 --> 00:02:58,110 this is how this works. 39 00:02:58,110 --> 00:03:01,790 However sc like this would be treated as a string, 40 00:03:01,850 --> 00:03:05,980 we need to tell mongodb that we're referring to this variable. 41 00:03:05,990 --> 00:03:09,940 Now one dollar sign would look for a field named sc, 42 00:03:10,310 --> 00:03:13,040 we add two dollar signs in this case, 43 00:03:13,040 --> 00:03:19,280 you don't see it that often because you don't have that many operators of this kind but filter which 44 00:03:19,280 --> 00:03:25,040 essentially executes a function over and over again on all the fields in exam scores or all the elements 45 00:03:25,040 --> 00:03:31,670 in there I should say, filter need such a temporary variable for every step it executes for us and we 46 00:03:31,670 --> 00:03:35,230 can refer to this temporary variable with the double dollar sign 47 00:03:35,280 --> 00:03:42,470 syntax then. Now with this approach, we would not get the result we want though because you have to 48 00:03:42,470 --> 00:03:49,690 keep in mind that exam scores is in the end and I can show this to you again, is in the end 49 00:03:49,700 --> 00:03:57,890 an array of embedded documents where you have a difficulty and score. Now sc here would refer 50 00:03:57,890 --> 00:03:59,920 to the overall embedded document, 51 00:04:00,170 --> 00:04:02,150 now I'm interested in the score though, 52 00:04:02,450 --> 00:04:10,040 so I have to check sc.score here in my greater than condition. And with that I can take that aggregate 53 00:04:10,310 --> 00:04:12,840 function, execute it 54 00:04:13,040 --> 00:04:18,940 and we should get arrays with only scores greater than 60, 55 00:04:19,010 --> 00:04:20,340 so you can see that here. 56 00:04:20,910 --> 00:04:23,840 I get one document with scores greater than 60, 57 00:04:23,840 --> 00:04:26,610 then we got another document with scores greater than 60, 58 00:04:26,620 --> 00:04:29,870 there we only got one exam, hence we only got one object in there 59 00:04:30,020 --> 00:04:32,130 and then this last one. 60 00:04:32,150 --> 00:04:34,030 So this is how filter works, 61 00:04:34,100 --> 00:04:41,270 filter allows you to filter arrays in documents inside of the projection phase.