Let's look into our friends in their raw form again and let's now say we want to transform exam scores
to be an array where we only see scores higher than 60.
Now this can also be done in the projection phase because I want to transform every single french record
I don't want to group by anything,
I just want to transform the array in there. And to achieve this, I'll add exam scores again, you could name
this differently though,
this is up to you
and in there, you can now use the special filter operator.
Now how does filter work? Filter allows you to filter out certain elements in an array and only return
these that fulfill a certain condition,
so here I would want to filter for the score being greater than 60.
Now how does this work?
First of all you need to add an input here and that is the array which you want to filter,
in our case we could say it's obvious that I want to filter an exam scores because I will store the
result in a field named exam scores
but keep in mind, you can name this field however you want.
So therefore filter needs to know where is the input array and the input array is exam scores with a dollar sign
because I'm pointing at a field in my document.
Then we assign a temporary name,
this is a so-called local variable which will be used inside our filter expression which we'll write
in a second,
you can name this however you want,
I'll name it sc to not repeat
scores all the time
but you can name this however you want. The last element we need
or the last field is the condition, cond.
Now the conditions here can take a bunch of expression operators to which you also find a link in
the last lecture of this module and I'll just bring this into a new line to make it easier to read. The
expression operator I want to use here is greater than, now greater than here works a bit different than
when we used it in match or find. Here greater than simply takes an array of values it should compare
and this makes sense because we are now in the context of another operator, so we can't compare exam
scores the whole array or anything like that,
instead here what I want to compare inside of my filter expression is of course the value of this temporary
variable which will refer to the different values in my exam scores,
so you can imagine this being a function that gets executed for every exam score in my exam scores array
and then every value will temporarily be stored in here so that we can compare it.
So here in greater than, I will check if my sc variable is greater than 60,
this is how this works.
However sc like this would be treated as a string,
we need to tell mongodb that we're referring to this variable.
Now one dollar sign would look for a field named sc,
we add two dollar signs in this case,
you don't see it that often because you don't have that many operators of this kind but filter which
essentially executes a function over and over again on all the fields in exam scores or all the elements
in there I should say, filter need such a temporary variable for every step it executes for us and we
can refer to this temporary variable with the double dollar sign
syntax then. Now with this approach, we would not get the result we want though because you have to
keep in mind that exam scores is in the end and I can show this to you again, is in the end
an array of embedded documents where you have a difficulty and score. Now sc here would refer
to the overall embedded document,
now I'm interested in the score though,
so I have to check sc.score here in my greater than condition. And with that I can take that aggregate
function, execute it
and we should get arrays with only scores greater than 60,
so you can see that here.
I get one document with scores greater than 60,
then we got another document with scores greater than 60,
there we only got one exam, hence we only got one object in there
and then this last one.
So this is how filter works,
filter allows you to filter arrays in documents inside of the projection phase.