- [Instructor] MongoDB documents could store a wide variety
of data in a number of different formats
which often makes development easier.
So far, we've mostly worked
with text or number fields in our documents.
These are much like the basic types you might be
used to in a relational database
but what if we want more flexibility?
Take this recipe for example.
When we think about it,
recipe contains a lot more than just text or number data.
In fact, it has a lot more structure
and detail that we can see
on the printed page and categorize in our minds.
But how do we store it?
Sure, we could just copy and paste list of ingredients
and the directions and store them in a large text field,
but then we'd lose a lot of granularity
and options for what we could do with that data later.
For example, what if we need to change the measurements
from imperial to metric?
or change serving sizes?
In a traditional database,
at this point, you'd need to start thinking
about refactoring this data
and about normalization, denormalization, et cetera.
With MongoDB, we don't have to do that.
With MongoDB, we can mix and match data types together
and pretty much store this recipe exactly
like it is on the page, but also
in a way that will let us do really powerful things later.
At this point, you might be really starting to
understand why they call these documents.
Let's start with the directions.
There are steps, and the order matters,
sort of like an array?
To see how we could store this,
I'm going to open up some example files
I put it in the lessons slash 2.3 directory.
In here you could see there's
a directions dot JSON, and ingredients.
I'm going to go ahead and open these in VS codes
so they'll format it nicely.
You could see with the directions, we have a title,
and then I've just actually stored a flat out array
for the directions.
Roll the pie crust, make the filling, bake.
(laughs) Of course, real directions
would be different than this,
but of course this is just an example.
We can literally represent this with the JavaScript style
array and store it in our document.
Now let's move on to another part of this recipe.
The ingredients.
Oh, for ingredients dot JSON, you could see I've expanded
on this document.
We still have our title and our directions,
but starting here in line eight, we have ingredients,
and again, it's an array, but this time, instead of an array
of strings like the directions, it's an array of objects.
Not only that, that object has objects inside this.
You can actually think about this
as its own document itself.
The nice thing this lets us do is store ingredients
in their units together yet separately.
So we could, for example,
change the serving size or the units of measure later.
As you can imagine, there are a number of ways to do this.
This is just one example.
I'll also open up a more complex example of this.
To do that, I'm going use MongoDB Compass,
free GUI from MongoDB.
You can see the links of lesson notes
for more on how to find this.
This one is a full fledged recipe.
It has a title, calories,
preserving, cook time, a description, directions in an array
ingredients in a more robust object array, has likes,
like count, ratings, et cetera.
We're going to talk about how we're can start organizing
some of these documents into collections.
Lastly, let's show inserting
a document that we want to create.
So to do so, we can use the insert or insert one
or insert many, but for this example, I'll use insert.
So in this test collection, I'll use insert one,
and we'll give it a document to insert.
So we can say "Title test doc," and that's it.
We'll just have one document with a title,
and it'll say "Acknowledged true," that it inserted
and it gave us an object ID, which will be the
object ID of the document that was just graded.
So if we do "Test dot find"
you'll see a document with a title of "Test doc"
and the auto-generated object ID
for your underscore ID value.