So in the last lectures we learn about embedded or nested documents and arrays and this gives you a lot
of flexibility to structure your data in the way you need it to be structured,
also something I'll dive into deeper in my data schema and modeling and relations module in this course.
One thing I want to do before I close or finish this module is I want to show you how you could access
such structured data
and let's start with the hobbies here.
You can of course output that by let's say finding Albert with find and then I'll look for name, Albert
Twostone, like this,
now if I pretty print that, here's Albert.
We can now access .hobbies here because find will give us that person and hobbies should give us
access to the array here
then, though one important thing, we should use findOne here
so that mongodb knows we'll only have one element, so that we can directly access the fields of that
element after fetching it,
so after the closing parentheses.
With this we indeed get the hobbies output here, sports and cooking and we can even search for that. If we
want to find all passengers with a hobby of sports, what we can do is we can access hobbies here which
is an array
and look for sports and I'd put that in a pretty way
and here we find Albert too, the entire document not just the sports entry because we're looking for the
entire passenger documents where hobbies is equal to sports and mongodb is clever enough to see
that hobbies actually is an array so it will simply look if that array has one element named sports and
then it gives us the entire document as a result,
so this is how we can also query such arrays.
Now how can we query objects?
Remember we had nested objects, nested documents in our flight data
so here if I find all, we had the status. Now if we want to find all results with a status description
of on time, we can use our flight data and find and now in the filter,
we want to look for the status but the status,
well we're interested in one of the fields of that nested document. For this, we can use status dot
and then the nested field,
so you use dot notation to drill into your embedded document, so .description. Important when using
that,
so when having a dot in here, when using such a path to a nested field, you need to wrap the entire
term with double quotation marks though otherwise it will fail
and now we can check where is this equal to on time.
If I now pretty print this, I get both my flights because both have status description of on time, you
get no error
instead this query works,
so we can drill into embedded documents with that dot notation. And obviously we can go one level further
as far as we need to,
we can also drill into details.
So we're looking for our details here and we want to find all flights where details responsible is Max
Schwarzmuller,
we can do that too.
Now I'm drilling into details responsible and I'm looking for Max Schwarzmuller here and I again find
both flights because both have Max Schwarzmuller as a responsible person.
So this dot notation is also something you'll see a lot in this course as this is an important syntax
that allows you to look into embedded documents in your filters.
And this is it for now,
we'll dive much deeper into all of that and how to write efficient queries and how to access your data
and work with it,
we'll dive into all that throughout the course.
These are the core things you got to know about how mongodb works, how you could work with it, how
you can store update, delete and read data and how you can also structure your data.
Now as I mentioned, we'll have a whole module where I dive into how to optimally or cleverly structure
your data and how to work with related data
but this is now a good start.
Let's practice this before we learn about a different way of looking into your data.