Time for more interesting projections but now projections that work with arrays or are related to arrays.
I'll stick to my friends dataset for this,
so let's quickly peek into it and let's work with the exam scores
now. Let's say you only want to output the first value of that array instead of all the exam scores,
so it's time for a projection of course.
So let's add a project phase here and in projection,
I'm not interested in the ID but I'm very interested in my exam score and I'll name it exam score
and not scores because I'll only output 1.
Now to output one, there is a helpful operator and this is of course the main thing I want to show you here,
the slice operator. The slice operator allows you to get back the slice of an array,
so how does the slice operator work?
It takes an array itself,
the first value is the array you want to slice and you could hardcode an array in here, that would be
possible but of course I will point onto a field with $examscores,
so I'm essentially pointing at this field. And now I only want to get the first element and for this, I
pass one as the second argument because slice takes in its basic form two arguments, the array and the
amount of elements you want to get out of the array
seen from the start.
So here with this, if I take this command and I execute it, you see now I only have my exam score or one
exam score per user and that is the first exam score, we can validate this with the last user,
the first one should be difficulty three and score 75.1
and that is the case here to. Now
sometimes you want to you have let's say the last two, for this you can actually use a negative value
here, -2 because then we'll start at the end,
if I now paste this in, you see I get the last two scores per user,
so this also works or you want to get one element but starting at the second element, then you could
also use a different syntax with three elements in the array,
two and then one. This means start at position two and then give me one element and if I now execute
this, you see I get back some exam scores and which scores added,
well these are the last scores because indexes are zero based,
so if I start at two, I will skip 0 and 1,
so the first two elements, I start at the last element
therefore and then I take one element,
so I get back the last element.
So these are the different ways of using slice and that can be very helpful for transforming an array
or getting just the values of an array
you need.