In this video.
Let's talk about another way of relating data, and that is by referencing when we say by referencing,
what does it mean?
It simply means that we are going to save the ID of a document into a field and we use a special type
called Mongoose dot schema, dot types, dot object ID, meaning that we are going to save the ID of
a document, but for embedded we save the entire object into a field.
As we see in this slide.
So here comes the question when do we need to use the embedded and when do we need to use the reference?
Here is the answer we use embedded.
If you know that in future that particular field will not grow too much, for example, a product and
a review.
In that way we can use the embedded way, but a post and a command.
We need to use the reference because a post can have millions comment as compared to a review.
So the best way to go is to use the referencing, and the referencing is the most recommended way of
relating documents.
So let's see them in action.
So back to Visual Studio code.
I have cleaned up the previous code and now it's back to default.
In your case, we can make a copy for future reference.
So for the post schema, this what we have the title and the contents.
Likewise the user.
We have the full name and then the country and inside the post.
Right.
You can see that this is a normal way of saving the command into MongoDB.
Likewise, the function to there is nothing new here.
Likewise the user too.
You can see that we are saving a user document into DB and here we find the user.
So these two modules are not related.
So let's see how we can relate these two documents.
Here is a clue.
We are creating a blog application, meaning that a user can create many posts.
So let's see how we are going to model that.
So let's get back to the user model.
So for this we are going to save the post, the user is going to create as part of it.
Field So here we go.
We bring in the post and here is the clue.
This post must be an array, but what type of array?
So let me show you here we bring in the array and then the object.
And for this, the type is going to be as mongoose, dot schema, dot types, dot object ID and the
next field is going to be the reference.
So which model are we referencing?
We are referencing the post model.
Make sure that you type the exact name as the name of the model as we see here.
So now we are done with the user.
And also if you find a post, we want to know what user created that post.
In that way we need to go to post and then add the author field.
So here we go.
We will add the author or the user for this.
Let's make it as order for this.
We can use the entire user because a post might have only one author.
But the ideal way to go is to use the object ID.
So the type here is going to be as we did for post mongoose dot schema, dot type dot object.
ID take note about the common casing.
And we also want to reference which model that is a user model and that is it.
We are done with the relationship.
So now let's get back to the root and create a post.
So inside the client, let's go ahead and then register a user and now I'm going to use Joe and then
country Ghana, I'll hit send and now you can see that we have a property called Post which is an array
and by default it is empty because this user hasn't created a new post yet.
Before we continue, let's finish up this one.
This auto field must be a required fake, meaning that we need to look in before you can create.
So let's add required to.
But for a post on the user that I'm going to be optional.
So here we go.
So back to the post and points.
Let's go ahead and create, but make sure that we copy the ID of the user.
So let's go ahead and create that one.
So back to the post route here and let's add the payload that we want to create.
We need the title of the post and let me call this one as a HTML.
And we need the content of the post and let's say the best posts ever.
And when I hit send, you can see that I'm going to get some validation error.
Simply mean that this sort of field is required.
So let's go ahead and then do that one.
So let's add a property called author.
Well, if you're doing production application, we can get the author for the request object because
we haven't done authentication.
That's why I'm passing it manually here.
So I'll pass that one.
And next is our hit save.
But before saving, well, we want to save the post this user has created into the field of this user.
So before we save it, let's finish up the logic inside the post route.
So here we go.
So let's lay out the steps here.
So step number one is we need to find the user by ID and remember we are passing the user as author
so we can go ahead and then find the user.
So here we go.
Find the.
User and as simple as cons user found is equal to our weight.
And then the user model dot find by ID and I want to find buy rec dot body dot author where you can
structure the body object.
So here we can check that if there is no user found, then we can go ahead and then return and send
some Jason data to the user by saying that message and user not found.
Otherwise we can go ahead and then push the seatpost into the field of the user post.
So next step is step number two is save the created post into the user's post failed.
So here we go.
Now on user found, we have a property called Post.
Did you see that?
And one that we are going to push the post ID for this one.
We are going to save the entire post.
It can happen that a post might have less say more properties here.
But for this case we are just going to save the ID only.
So for this we can use the entire object.
And behind this scene Mongoose is going to use the ID only because we specify as an object ID or we
can specify also as the underscore ID.
Then the next step is that because we have made changes to the user documents we need to receive.
Great.
So here we go.
And wait.
And then user found dot C and that is it.
So with this one, I can go ahead and then make the request.
So check it out.
When I hit st this time around I get some error because I'm not passing in the user.
So here I'm going to be as the author and going to be as rec dot body dot author.
So now moment of truth.
Let's hit st.
I guess something now is loading.
Let's wait for and here we go.
You can see that now we are waiting this post by a user.
So if you go ahead and then fetch all users.
Now let's click on fetch all users here.
And this time around we have a field of post and we have the ID of the post, but not the entire document.
How awesome it is.
Again, let me prove to you before I end up this here, and like I said, that we can save the entire
object and behind the scene, Mongoose is going to take the ID only.
Let's save it.
And now let's go ahead and create a new post here.
This time around, they change.
This one to note, J is now the same author Let's hit send.
And I got the response if I'll fetch or uses.
You can see that mongoose is smart by taking the ID from the entire object.
How awesome it is.
So here comes the another question.
How will I know the real post here?
Likewise the post.
How will I know the actual user than using the ID as we did before?
We are going to use the populate method.
So now back to the user user route here on fetching, we are going to populate the posts.
So here you pass in the post, make sure that this value or this variable is on the user model because
we have it already as post.
So now let's save it and let's fetch our user.
And this time around we go back the to our post than the ID.
So upon requesting that's where we are going to go to populate it.
How awesome this one is.
Likewise, the post may be asking How will I know which user created this post?
So as we did for user, we are going to populate that one and we use what is called author on the post
field.
Let me show you before I make the request, which is this.
So now let's save this one and now let's go ahead and then fetch all posts.
Now this is the end point to fetch or post here and let's hit send.
And now we have two posts.
One is the HTML with the author as this and then the post number two as node and then the author as
this.
And you can see that this post caused the IDs.
So how can we make public to this one unless we do what is called nesting population?
So back to the post route here to be able to use the populate, you are going to change the way we are
doing this.
Well, for this one too, we can achieve the same thing by using object and providing a path and the
value must be a property on the post.
So going to be the same thing.
So let's save it this time around you're going to get and we got the same or not we need to find the
post this one so let's thank and you can see that we have the same result but for this one we have more
control how we can populate what is called nesting document.
So this is the post route.
So here we bring in populate again and as an object and then we specify the path.
And the path this time around is called post and we can go ahead and also specify the model and this
one is post model.
So save it and now let's make the requests and here we go.
And this time around we go back the actual author and the post to we go back the actual post.
So this is what we call referencing and this is the most recommended way.
And I advise you to stick to this method.
All right.
In this video, let me show you one cool thing about Mongoose, and that is virtual properties.