So let's get started with that,
with implementing this kind of structure.
And for this, I first of all
wanna clean up my existing data.
So for that, I'll reach out to restaurants
and run deleteMany and actually pass in
an empty object here,
which is a quick trick for deleting all the documents
in a collection.
You'll of course, rarely wanna do that
because it really erases everything
but here I do wanna do that, to clear it.
Now, with that, I deleted the last remaining restaurant,
which I had.
So now we can start from scratch again
and again, I will start by actually not setting up
by my restaurants but my types.
Again, having that extra types collection is optional
but I explained why I do wanna have it in the last lectures.
So here, I now wanna start by inserting types
and here, I'll insert one new type at a time
and for every type, I wanna have an ID,
which is automatically generated and the name of the type.
So here, since the ID is automatic generated,
we just type name and then this could be German.
We confirm this and then we may be also add Italian
and then maybe also American
and whichever other types of food,
of cuisines you wanna add.
Now with that, with these types inserted,
if I run db.types.find,
we can list all these types and that's now what we got here.
Now we can move on to the restaurants
and here we can use restaurants and again,
insert one new restaurant here.
So again, we define such a JavaScript object, so to say.
The ID will be added automatically
but in addition, we wanna add a name
and then this nested address
and then also the nested type data.
And therefore, I'll start with the name.
Again, let's say I have the Munich Schnitzel House,
then the address, which is this nested object,
So another pair of curly braces,
and then here for that address,
we have the street, food street, let's say.
Then the street number, there is a line break
but again, it's one command.
So a street number, which I'll turn into a string here
because we could have street numbers
like this with annotations.
We have the postal code
and here I'll store this as a number actually, like this.
We could also store it as a string
and that might be better
in case it starts with a leading zero or anything like that
but to also show you how you can store numbers,
this is how you would do it.
You create a number as you would do it in JavaScript
without any quotes or anything like that.
Of course, we also have the city,
which in my case here is Munich.
And then I also have a country,
which in my case here, is Germany.
So that's now the address.
Now, after this address object, this nested object,
I also wanna have the type field
and now that could just be German
but for the reasons mentioned as before,
I will store it actually as a nested object,
where I point at the type ID may be stored like this,
this key name is up to you
and that now should be the ID of the cuisine
I stored in my types collection.
For this, I'll create such an object ID here.
We could also just store it as a string if we wanted to
but I'll create an object ID
by executing this object ID function
and then I'll grab the ID here with the quotes,
from the German type and insert this
between the object ID function.
And then separated by number, comma,
we can insert our name,
so that we don't need to manually
fetch this from the types collection
but so that we have it right here
when we do fetch all our restaurants later
and that's German.
And of course here, I'm entering it on my own again
but as I mentioned before,
if this would be created through some web application
where we use some form for creating restaurants,
then we could fetch the possible allowed values
from the types collection
and pre-populate a dropdown with those values
so that this is actually not a manually entered field.
So that's really just an exception here
because I'm using this shell.
And with that, the first restaurant is done.
If I now hit Enter, this was inserted
and now we can quickly add a second restaurant here.
And let's say this now actually should be a burger house.
So that's the Berlin Burger House.
And the street here is Ham Street
12, let's say, postal code is maybe this.
The city of course is Berlin, country's Germany
and now the type is American, so I'll wrap this ID here.
And then, actually clear this ID
from the object ID function
and paste in that new ID
and replace German here with American.
And then again, hit Enter to insert this.
And with all of that done,
if I now quickly find my restaurants,
here are my restaurant documents
and you can see those nested objects in them
that hold that nested data.
And here, we really see the difference
between no SQL and SQL
because with no SQL, related data is often stored together.
As you can see it here,
instead of split out across multiple tables or collections.
Now with the restaurants added,
I also wanna add some reviews and for this,
this is the plan I came up with.
I wanna have a separate reviews collection
where I have that reviews data
but in there, I'll store a link to the related restaurant
and also the name of the related restaurant,
so that when we fetch the reviews,
we don't need to separately go to the restaurants collection
just to get the names of the related restaurants
but that instead the names are stored together
with the reviews, so that whenever we show
a list of reviews, we can run one query, one command
to get all the reviews
and then also have the restaurant names
already with that response.
So therefore, we can now go to db.reviews
and actually, first of all,
I'll reach out to restaurants and run FindOne
with an empty object to simply find that first restaurant,
because I'll need its ID in a second.
And then I'll go to the reviews collection,
which doesn't exist yet
but which will be created on the fly, as you learned
to insert a new review, like this.
Now here for the reviews,
we wanna have the name of the person who left the review.
The ID will be created automatically, as you learned.
We wanna have the rating,
which should be a number between one and five.
And then, we also wanna have the text
that describes the review, or that explains the review.
Then a date when the review was left
and then the restaurant data.
Now, let's add their reviewer data
and that could just be something like Max Schwarzmuller.
Alternatively, of course,
that could also be some nested object
where you then have a first name, Max
and a last name, Schwarzmuller.
It really depends on how you wanna fetch
and store that data.
That depends on the application you're building.
To show you that you could store it like this,
I will store it like this
but just storing it as one string,
instead of this kind of nested object
would also have been fine.
And theoretically,
you can also store it in different ways
in your different documents in that collection,
though even though you have that flexibility,
you'll of course typically wanna use a similar structure,
so that when you fetch the data,
you can use it in one of the same way
and you don't have to check if it's now stored as a string
or as a nested object.
But here, I'll go for the nested object,
simply to, again, practice this nesting aspect
and then separated by a comma,
we can add the next field.
And that could be the rating,
which should be a number between one and five
and let's say, here, it's a three.
Again here, I'm entering this on my own through this shell.
In reality, this would probably be coming
from some application and in that application,
you could make sure that no other values
than one and five are inserted.
Now we also wanna have the text.
Again, please note the line break.
It's text here as a key name.
And the text then, in my case here would be a string.
Unlike in SQL, there is no difference
between a short or a long string,
you can simply store
as much text here as you want in the end.
So here I'll add, this was okay,
could be better, as a text.
Because as a good German,
I of course always have something to criticize, right?
So, next key value pair would be the date.
Now we haven't done this before,
so that might've been something you struggled with
but if you ran a search for MongoDB date,
you'll find the official documentation article here.
And in there, you learn
that you would then create a date
just as you do it in JavaScript.
And you can, for example, pass a date in this format
to this new date function
to define a specific year, month and day,
or even add hours, minutes and seconds,
depending on what you wanna do.
Now here, I don't care about the hours, minutes and seconds,
I just wanna point at a specific date.
And hence here, I'll run new date
and to new date I'll then pass
2021-09-10, for example.
So that would be the date I wanna store.
Last but not least,
we wanna store data about the restaurant
to which this review is linked.
For this, I'll add a restaurant key
and that again is a nested object where I store the ID
of the related restaurant,
could also be named restaurant ID, of course,
or just ID, it's up to you.
And I'll create such an object ID here
and then grab this ID of this restaurant
for which I wanna create a review here
and paste in the quoted ID between object ID.
And then after this object ID value,
I'll add a number of key value pair
and that could be the name of the restaurant,
again, so that we don't have to separately fetch this
if we wanna have a list of all the reviews
with the restaurant names included.
So I'll just paste this in here as well and then hit Enter.
Now this was inserted and therefore, now again,
if we run reviews.find, we see our review document here.
Now of course you can play around with that
and insert more data.
And indeed, I would strongly recommend that you do so.
All the practice querying data,
filter for certain conditions
and then all the practice update and deleting data,
we'll do that together in the next lecture.
So practice the things we did learn up to this point
before we then wrap up this section
to then use MongoDB with node JS in the next section.