Welcome back.
In this video, let's finish up the embedded way of database relationship.
Good.
So here we go.
The first step is that we need to find the author who is creating the posts.
And lucky for us, we are passing in the author as the ID here.
So let's get back to the post.
So the first step here is let's have some roadmap.
The first step is what?
Find the author because we need the author ID before we can create and we can get that one from where
they request.
Dot body dot author which is this.
So now here we go.
Cost author is equal to let's await the requests and then user I have the user model dot find by ID
and I'll get it from rec dot body dot alpha as that.
Now here we assume that the user is being found.
Well, we can check if there is no user.
But for this case let's ignore the error handling.
So at this point we assume there is a user.
Step number two is we need to create a new.
Post and that is what we are doing here.
So remember creating a post you need to pass in the author.
That's how we have specified here.
So inside the post, let's provide the author and the author.
We can get it from when rec.
Dart body.
Dot author and we are done with that.
The next step is remember when creating, we want to push all the posts the user has created into the
fields of this user.
So here we go.
After creating the step number three is that push.
The Post.
Into the users post field as that.
So how are you going to do it?
Remember we have the author here and on that we have a field called Post which is this.
So here we go when I be the author, sorry, author.
Instead we have a property called Post and then we are going to use a real method called push and we're
going to push the entire post into that because here we want to save all the post into that and this
is what you call the embedded meaning.
We are embedding the whole contents of a document into one field.
In this case it is a post document, so let's pass in the saved post.
So because we have make changes to the user's document, we need to save the author.
So step number four, I'm going to be re save.
The author.
And here we go.
We are going to await the process and going to be the author.
Dot save.
I is that good?
And that is it.
So lastly, we need to send the response step number five and maybe send the response to clients.
And that is what we are doing it here.
So now moment of truth.
Back to post here.
Now let's check the response after clicking.
Now have a look when I hit send, let's wait.
And this time around something is different.
On the post we have a field called author but instead it is an ID.
So this simply means that we are saving the author or the user who is creating this post onto the post
field.
And if I fetch all users, I'll be able to see the post that this user has been created.
So let's click on all users to fetch all users.
So here is a request for users.
Now let's send let's have a look.
This time around we have the full name country and inside the post we have one post being created by
this user.
All right, I can go ahead and create a new post with the same author, I can say.
Express, Jess.
And I would say it is excellent.
Let's make the requests now and let's check it out here and let's fetch our users and let's see, we
have two posts.
Post number one is not jazz and post number two is called the Excellent Express.
That is it.
And if I fetch all posts, I'll be able to see the user.
So here we go.
When I made the request, we see that I have to posts with all five property and this one ask that.
So I think you would be thinking that you want to get the real data about this author by not the ID.
So on the author, like I told you that we can save the entire user into this by making use of this
one as an object.
So inside the root we can use the user found, which is this one.
Let me show you here.
This is a found and then we save it inside the author here or let me show you the recommended way.
So back to the post model.
Instead of just doing this way, we are going to use a special type called Mongoose dot schema.
As I said before, now this is a time schema, dot types, dot object ID.
They are all common cases with the exception of Mongoose, and for this one it means that we are going
to save the IDs of the user.
Only the next property is going to be as RDF, which means reference, meaning that which one are we
referencing?
In this case we are referencing the user model because we are saying that this author represents the
user, meaning the user model or the instance of the user.
So make sure that you type in the exact name as your model name for users.
In this way we have a special method to get or to convert the IDs into real object.
So inside the rand for finding out how we change the query like that, we can also change what is called
populate.
As the name implies, the populate is used to convert all the IDs into the normal object and here we
provide the property name.
If you go to the post model here we see that we have the author field and this is what I want to come
back to normal user or populate.
So now inside the root our mix of what's the author.
If I go to fetch all posts here this time around now let's check when I make the requests.
Well, we see that now the IDs has been converted into the normal user.
How is that?
How awesome it is.
So this is what we call the embedded way.
But for some reason too we are using reference, but in this case it's not the normal reference.
But I've given you a little bit of referencing.
In the next video I'm going to show you the most recommended way.
That is what you call the referencing by using one to many relationship.
So for the same concept you are going to use different methods to achieve the same idea or concept.