In this video.
Let's talk about how we can relate to or more document together.
And the first hype is what we call embedding method, meaning we can relate to or more document together
using this method called embedding method.
And I will explain to you as we move on.
The next hype is what you call referencing.
So among these, which one should we use?
Well, let's dive in more to talk about each of these type.
Let's start with embedding method.
So what is embedding way of relating document together?
Let me show you this picture.
Well, to understand what is called embedding in this case, let's say we are developing a blog application
and we have two collections.
One collection is called the user collection, and this is what you call the user schema.
And you can see that for this user schema, we have these properties called Full Name Country and then
post where I will come up to this and let's say that you also have two posts post one and then post
two and each post it has a title and then ID call post one ID and then post to ID.
So these two posts are from post collection and then this one is from where user collection.
So it means that we have two collections.
So how are we going to relate?
Simply means that if I want to know how many posts are all post that has been created by a certain user,
we need what is called relating document together and the first type is what you call the embedding.
So what does it mean?
Let's have a look at the user's collection or the user schema here.
If you look at this field, we have post and in there we have arrays of post ID and the post ID one
represent this post and then post number to represent this post to.
In this case we are relating to document together.
So it means that if I fetch user, I'm going to see all the posts that I have created and this is what
we call the embedding, meaning that we are embedding the IDs into the fields of post of the user,
and for embedding we can take the whole object and then place it inside the post array than the ID.
We can use the entire object and place it inside as that, or we can use the IDs and if we want to fetch
we are going to look through the collection and then convert all these IDs into normal posts.
And this is what we call the embedding way.
But guys, this option has some limitations and drawbacks.
Let's take it that if I want to find or in my profile page, I don't want to fetch my post, but because
we have embedded, it means that any time we make requests to the user or the post this user has made
will be fetched.
Let's take it that this user has created 1000 post.
You can see how we are going to consume resources.
And again, MongoDB has limitation on how many size of documents a particular field can take or a collection
can take.
Again, another limitation is that let's say that in future I have updated post number one to post number
for a title.
So I need to come to my collection of users and also update that post manually.
Or I can use what is called middleware for Mongoose to do that work and wouldn't it be a little bit
difficult?
But in some cases you may need this kind of embedding in some use cases.
Case number one is that if you know that this field isn't going to consume or contain a lot of documents,
then this is the best way to go.
And again, if you know that the document inside this array is not going to be updated frequently in
the future, then this is the best way to go.
So what is the next type of relating the command together?
And that is what we call the reference end.
This is the most recommended.
All right.
So what is referencing again, let's say we have the same collection from the previous video.
We have user module or user schema, and then we have also the post schema.
But before I explain, let's look at the files inside each schema.
For the user, we have the full name called John Doe and then country called UAC.
And then on the post we have a title and then the user who is creating this particular post.
Well, it will say that inside we are using special syntax called mongoose dot schema, dot types,
dot object ID and we are referencing the user, which is this.
So when you are using referencing these two documents are independent, they are not related together.
But because we use what is called mongoose schema dot hypes, dot object ID and the user meaning the
reference we are using what is called the reference in way.
Now here comes the question that I know you may be thinking of.
Question number one is that how will I know which user created a particular post?
And it's pretty simple one.
You could see that we have the user here, meaning that when you are creating a post you need to provide
the ID and this one means the mongoose ID, meaning the user ID and you provide it here.
So right now we will know which user has created this particular post.
Right now, next question is how will I know?
How many posts have I created?
Because there is no field or post on the user model.
But behind the scene we call something virtual properties.
When we say virtual properties, they are properties that are not on this schema.
But when we execute a method, for example, find we can populate those virtual fields upon making a
request like find or find or find by ID and you will see those fields in the next video.
Let's start with how we can model a data where a user can create many posts and a post can belong to
a single user.