How can we work with array data? For that,
let me quickly find my movies and look into the structure.
Now we got two arrays here, one of them is the genre array,
now let's say we want to find all movies that have a genre of drama.
Now of course you know how that works,
you simply go to your query and there, you check that genres is equal to drama and this will look for
all documents that have drama in the genres array.
Now if I only want to output that drama genre then, I can add projection,
let me maybe do this in a cleaned up console, so I can add projection, so a second argument here and
here, I can use a special syntax and set genres.$ to one.
Now what this means is give me the one genre you found and therefore in my genres, I only have drama
now,
now these items will have more genres than just drama,
I only just fetch the drama,
I only just output that because that might be the only thing I'm interested
but the items behind the scenes will have more data, just as they have other fields too.
They have more genres too but with this syntax, I only find the first match for my query here,
if I had a more complex query for different genres, I would still just find the first match,
let me show this to you. If I have a slightly modified query and I'm looking for genres that have all
drama
and horror, I now only get back horror here. Now now this might look strange at first,
the reason for this is that all works such that it goes through the arrays and checks for the existence
of drama and horror,
so the first element to me both is well found when horror is confirmed to be in there too and that
is why we output horror because that technically is the first matching element,
drama alone didn't match anything,
the main thing here is that it doesn't return both but only one.
Now sometimes you could also have the case where you want to pull out some items from an array in your
document that are not the items you queried for.
So here's an example, for movies
let's find all movies where genres does have a drama
and of course if I execute it like this, we already know that result, we get the movies with the drama.
Now what I can do is I can add some projection here, so I can add a second argument to project my values
and say genres, well we know that I can use genres.$ and set this to one to include only
that and now only see the result of my query but what if I actually want to now see only the horror?
Well then here in the projection, I can set genres equal to and now I can use elemMatch here
and we saw that before but now it works in the context of projection,
it still works in the same way as shown before,
so this will find us the first element for which a certain condition is met
and here, my condition simply is equality to horror. If I now hit enter,
you see for some documents I have no genres.
So these are documents which will have a drama because that is what I filter for
but then I try to pull out only the horror part and that is simply not included and for others I
do have horror,
so these will be items that do have both drama and horror.
So I hope this makes sense,
this basically gives you more flexibility to control which items of an array are displayed in your result set
and which items of an array are important for your filtering
and now, these don't have to match.
So if you get cases where you want to filter for something in an array or for anything else on your
document, it doesnt have to be filtering on an array and you then want to pull out certain values of an
array based on some other condition,
you can add that condition with elemMatch here on the array you're working on,
so I hope this makes sense.
This filter here could totally go against something else,
we could be filtering for the rating here.
So here, I could absolutely filter for rating.average being equal or being let's say greater than
nine and then output that with projection
and here we see only one element, only in one movie or series with a rating greater than nine, seems
to have a genre of horror because it's empty for the others but now filter
and this projection are totally unrelated and that is important to understand here.