Now that we have learnt about Mongoose Population,
and how it enables us to populate one document with information from another document.
In this exercise, we will modify the express server that we have been working on so far.
In the dish schema that we have defined earlier, we had comments.
For the comments, we had the author field
that we use to store the details about the author.
In this exercise, we will turn the author field into a reference to a user document,
and we will use Mongoose population to populate the information into
the dishes document as and when required in order
to supply the information to the client.
Now the use of populate and Mongoose population should be done
judiciously so as not to cause too much of overhead on the server side.
Now in this exercise,
we're going to use it simply to populate the information
into the author field of our comments.
So, let's proceed with the exercise to learn how we use Mongoose population.
To get started on this exercise,
go to the project and open the user.js file.
So, in the user.js file,
we store the user schema.
I'm going to modify the user schema by adding in a couple of more fields in there.
One is the first name,
which will be of
the type string and
the default would be an empty string.
So, the first name as the name implies,
stores the first name for the user and then we'll have another field called last name,
which is also of the same kind.
So, I'm just going to copy these two pieces of information and then copy it in
here and so now our user document will contain,
in addition to the username and password,
username and hush and salt that we have seen earlier,
that is automatically added by the passport local Mongoose module.
We will also have the first name and last name for the user being defined here.
Later on we will see how we would initialize
these values by modifying the user's registration process.
Now once we have completed this,
so this way the user's information
can simply be retrieved by looking up the user document here.
So, now that we have the information about the user in the user document,
going into the dish schema,
so going into dishes.js file.
In the dish schema earlier,
we were storing the author of the document in the form of a string here.
Now we're going to be taking advantage of the fact that we
have the support of mongoose population.
So I'm going to turn the comment field from a string into
mongoose schema types object ID.
So, this way, sorry, wrong field.
I meant to turn the author field into
mongoose schema types object ID.
So, the author field now instead of storing a string,
will have a reference to the user document.
So, when I turn the author field into this type,
then the second property that I defined here will be a reference,
which would be a reference to the user model.
So, this way, we are now going to be
connecting this author field and this author field will
simply store a reference to the ID of the user document,
instead of storing the details about the author in the form of a name.
Now when we do that,
we can use mongoose populate to populate
this information into our dishes document whenever required.
So, with this modification to the dishes schema, in dishes.js file,
we'll now update the dish router to use the mongoose population.
So, going to dishRouter.js.
In dish router, recall that when we were getting a dish here,
now when you get the dish here,
we say dishes find then.
So, right at that point,
we'll say dishes find and we will say after this, populate.
So, we are using the population support in mongoose
and we'll say populate comments author.
So, by stating this,
we are saying when
the dishes document has been constructed to send back the reply to the user,
we're going to populate the author field inside there from the user document in there.
So, this call to the populate will ensure that
the other field will be populated with the information as required.
Similarly, going to the dish ID here,
even in the dish ID, same thing.
We will say populate and comments author added
into the dishes find by id in
the get of the /dish ID endpoint.
Similarly, in the comments also when we retrieve the dish,
we'll say populate comments author here and the
same thing also in the dish router,
dish ID comments, comment ID also.
The bigger populating this information there.
Now of course, what this means is that when you are posting the dish,
earlier you are including the author information in the body of the message.
So, now here when we try to push the comment into that,
so this post corresponds to the dish ID comments field.
So, this is how we were posting a comment to a specific dish.
So, now in this post,
since we are not storing the information about the author anymore,
so what we need to do is when we push the item into the author field there.
So, here when you're populating the information into the dish,
we will have to first-