Let's now dive into querying arrays a bit deeper because there are multiple things we can do when it
comes to querying arrays
and there are special operators that help us with querying arrays too.
Now first of all, I'm back in the same database I was before
and please note that I will also work with the data we use in the assignment,
so if you skipped the assignment, at least download the attached data and import it as described in the
first or as requested in the first assignment task, you don't need to complete the rest but you need
to have that data in your database.
So first of all, I will however switch to my user database here and in there,
remember we had a couple of users,
let me pretty print them,
we had three users here.
Now the users had hobbies,
now let's say we want to find all users who have a hobby of sports.
Now one problem we have is of course with that structure, where hobbies are embedded documents and not
just strings, that it won't work if I search and pretty print for hobbies being equal to sports because
there will be sports in the hobbies
but that's just the value of a field of an embedded document in the array.
So if I hit enter, I don't find any documents,
how can I now find all hobbies that are sports?
Well of course we could search for exact equality and pass the document here and search hobbies for nested
documents, embedded documents where the title is sports but if I hit enter there, I also find nothing because
we have no hobby documents in the hobbies array that look like this,
our documents also have a frequency.
So yes I could of course add the frequency here and I could look for a frequency of two
and now I would find Anna but I don't find Max because for Max, the frequency is three. So this does
not really get us anywhere,
we can't really use this way of searching for sports
but what if I really just want to find all documents that have an embedded sports document no matter
what the frequency is?
Well this is all pretty simple to do in mongodb, we just need to change our query a little bit. Instead
of just looking for hobbies and then looking for an equal document,
we can use a path here, wrapping it in double quotation marks with dot notation and I can look for
hobbies.title being equal to sports.
Now the interesting thing here is title is of course something we can see on the documents that are
inside the hobbies array, so we can act as if hobbes would hold just an embedded document and dig into
properties of that embedded document
even though here we got multiple embedded documents in an array but mongodb understands this syntax
and what it will do for this query is it will essentially go through all the elements in hobbies and
for each element, it will dig into the document and compare title to our query value, so to sports. And
therefore if I
now hit enter,
I do find Anna and Max. And this is a super useful syntax for querying array data that you absolutely
should keep in mind, that you can use this path embedded approach, not only on directly embedded documents,
so if you have one embedded document in a field but also, if you have an array of embedded documents
and then you can use the same query world with greater than and everything you want as before, you don't
just have to look for equality,
you could have a document with greater than, lower than, with an expression, whatever you needed.
So that is possible here too.