Now you are also not limited to querying top level fields, if I quickly find one element so that we see
the structure again. You see in this document,
we have a lot of embedded documents, externals, image, the network where you could see this,
the rating which holds one nested field, schedule,
so we have a bunch of embedded fields here,
we also have arrays and I'll come back to querying arrays too.
Now you can query such embedded fields as well and querying them is quite simple,
I show you the syntax earlier already,
you can run your find or find one query by passing a document
and now let's say we want to look for all series or all items here where the rating is greater than
7,
so for example, Glee should not be included.
Now we can do this by specifying the path to the field we're interested in,
so in our case that's the average yield instead of the rating embedded document. You specify such a path
by using double quotation marks around the path, this is required because otherwise the dot is considered
an invalid character and then you have your field that holds the embedded document,
so in my case that field name is rating, not ratings, rating, that holds the embedded document.
So rating in my case, dot and then the name of the field
and if you had multiple levels, let's say there you had the total rating and then you had the average,
this would work too
but I just happened to have an average field in there and that holds the value
I want to query.
Now we could query for equality
but as I mentioned, I want to find all ratings greater than 7 and that can be done like this,
it also doesn't matter if you use 7 or a 7.0 here, you can use both.
Now if I do that and I have pretty print my results,
I get back quite a lot of results but you should not find any item where the rating is below 7, that
should not be the case.
So there, you find an easy way of querying embedded documents
and as I mentioned, that embedding, that path can be as deep as you need it to be,
so you're not limited to one level,
you can add as many dots in your path as required to dig into that embedded document. And then you can
use all the queries selectors, all the operators on that embedded field.
You can also query arrays,
we got the genres array in our data which has some keywords for the different genres,
now let's say we want to find all items which have the drama genre.
Now to make this work, we'll use a find query again but the term here or the query expression of course
differs.
Now keep in mind, we're looking for the genres field, that is not an embedded document but an array,
the approach I show you would work equally for arrays
in embedded documents, of course you just would need to use the dot notation to reach that array in there
but then it's just a normal field.
So here it's a top level field named genres,
so let's look for genres here
and then let's try this, let's try looking for drama.
The casing here is important by the way, drama like this will not give you anything because there is
no drama, it always starts with a capital D,
if I look for it like this, I do get back results and all these results will have genres
that also includes drama and that's the important thing,
equality here with an array does not mean that drama is the only item in the array, so it's not like
genres is equal to just drama.
It's just equal to
there is drama in the array so this item does exist,
genres is equal to drama in that drama exists in the array but there may be more genres.
If you want exact equality, you can get that too of course,
if you want exact equality, you can modify your query to not look for genres drama like this
but now you include square brackets around that. That signals mongodb that you will look for genres
being equal to exactly that array.
If I now hit enter, I also find some documents and here
indeed, genres is just drama,
just an array with drama in there,
no other genres.
So this is really important to understand, this is how you can query arrays in its basic form either for
exact equality or for doesn't include this item,
both can be helpful depending on what you're trying to achieve.