So let's go from drawing to database action. I'll go to my blog database
and there I said when I want to use two collections, let's start with the users collection, there
let's say I insert
many users, we get multiple authors on our blog.
Now insertMany takes an array of documents where each document is one element we insert into this
collection, a user here can have a name, Max Schwarzmuller, can have an age,
29 and let's say an e-mail address, max@test.com which is not my real address.
I also have a second author, Manuel Lorenz, age 30 and also an e-mail here, manu@test.com, if I hit enter,
these two elements get inserted
and obviously we can now pretty print them,
this is the data in our users collection.
Now let's go to our posts collection,
so posts, there
I want to insert one post let's say, obviously we could insert multiple posts.
Now one post can have a title,
my first post, a post can also have some text which holds the actual content,
this is my first post,
I hope you like it
and this could be a very long text
as you learned, the text type is able to store very long text.
We might also have some tags here and that could be an array of strings like new tech,
whatever your tags are and now we come to the relations.
Now my relation here is that this post is written by a user,
let's name it creator
and this could be a list if you have multiple authors in your application,
let's say in this application and that is totally up to you of course,
we only have one author per host.
So my creator or author, however you want to name it here should be one of my users
and let's say this was written by Manuel, so I'll copy that objectId,
we could match it by any key of course but we should insure it's unique
and the objectId isn't sure to be unique and I'll therefore set this as a value for creator.
Now we also might have comments here and let's say we get a couple of comments there and each comment
then is another document,
so here I got a list of documents where I have some text,
I like this post and as another key here,
I don't just have the text but also the author of this comment
and that again would be the ID of a user,
so let's grab Max here maybe, grab that objectId here and insert this
as the author.
So here again,
I'm also using not an embedded user for the same reason why I'm not using it on a post, I'm not using
it on a comment either
instead here, I also use a reference.
Let's hit enter,
this worked and let's have a look at our post,
this is how it looks like.
And this post has the fields
I sketched out and most importantly, we have a reference relation to the person who created the post
but we got an embedded document or even multiple documents for the comments where each comment in turn
also has a reference relation to the author.
This is how our blog could look like in code,
so in the database and I also explained why I would structure it like this and this example together with the
previous lectures hopefully gets you into the right mental model on how to think about this and hopefully
helps you with your next projects to structure your data and your relations in a way that makes sense
for your application to your size of data,
the amount of data,
the frequency with which you change it and your overall structure or relation that is imposed on to you
by your application.