Let's now jump back to the user database and in there, just a quick reminder we got that user data.
Now let's say we want to find all users who have a hobby of sports and the frequency should be equal
to two,
so the frequency of the sports hobby.
Let's try this, let's reach out to our users and we want to find all users where this is true,
now how could this query look like?
Well we could use the and keyword and then we have multiple conditions and the first condition is that
for hobbies, whoops, for hobbies.title, we're looking for sports,
right this was the first part,
it should be sports.
The second condition could be that hobbies.frequency should be equal to 2
and that also has to be wrapped in curly braces
or let's say it should actually be greater than or equal to 2.
If I hit enter,
yeah find Anna, make sense,
she has sports and frequency two
and I find the whole document of course because find always gives me the whole document even if I'm
just digging in some array or embedded document,
it doesn't return the embedded document but always the full document which contains the embedded document.
So I find Anna because she has sports title two, frequency two, I also find Max because there, I got sports frequency
three.
Now let's say I repeat that query and modify it slightly
and now I'm looking for a frequency greater than or equal to three,
now what's that?
I still find Anna even though for her sports hobby, the frequency is two
but our query was that the frequency should be greater than or equal to three,
so what's going wrong here?
The thing is with this query, we're basically saying find me all documents where in hobbies,
there is a document at least one document with the title sports and a document with the frequency greater
than or equal to three,
it does not have to be the same document,
that is our issue here
and indeed for Anna, yoga has a frequency greater than or equal to three.
She has a sports element
so the first part is satisfied and the second part is satisfied with that other element.
Now of course it's not that uncommon that you want to ensure that one and the same element should match
your conditions
and for that, you have the elemMatch operator. So this query
and I'll just count it so that we have it still on page, this query can be replaced with another query
that should do what we want. Instead of and,
we specify our array hobbies and then a value which is a document where we use $elemMatch,
that
then again takes a document as a value
and here, we then describe how to match our element.
Now if hobbies were just an array of numbers let's say, you could immediately start typing greater than
30 but lower than 50 for example,
so that would be the case if hobbies would really just be an array of numbers,
not of embedded documents.
However here, we have embedded documents and in this case, $elemMatch is a document where you now describe
what to look for inside of the embedded document,
so here we're saying title should be sports and the frequency should be
and now here we can still write our old condition gte three.
So this is how we can now use $elemMatch on an array with embedded documents,
we essentially you could say describe how a single document should look like to match our query and
if it finds at least one document in the hobbies array that has a title sports and a frequency greater
than or equal than three,
it will include the overall document that contains that hobbies array in the result set.
If we got multiple elements in the hobbies array fulfilling this criteria, that would also be fine.
And if I now hit enter, I only find Max and not Anna anymore because Max is the only user who has a
hobby with a title of sports and a frequency of three.