In this video.
Let's talk about another important property or concept.
When it comes to mongoose, it's what we call virtuous.
So in Mongoose, a virtual property is a property that is not stored into the MongoDB.
But instead, we use the virtual house to do what is called computation or add more logic to our document.
So let me show you what I mean here.
So back to the model.
Let's open the user model.
Let's take it that we want to find the number of posts a user has created.
We can do it inside the root that is open, fetching, and then we can fetch a post by a user.
And on that we can use the array method on it to get the total post created by a particular user.
But we can achieve the same concept using what is called virtuous.
So let me show you what I mean.
So now let me collapse this one and let's come down here and let's say find the total post created by
this user.
So here we bring in the schema that is a user schema and we add what is called virtual grade.
So this property is not going to be persisted into the document and save it into DB.
But instead, upon fetching or querying, we can get back that property.
So for this, let's call this one as post count and we add what is called dot gate.
And for this we pass in a callback function.
But for this we are going to use normal function because we want to use this keyword inside that.
So here we go.
Great.
So how are you going to get it done?
For the meantime, let's retain some static value, say 20.
So with this one, when I make a request to find or to fetch a user, I will see a property called post
count, which is equal to 20.
So now let's check it out.
When I hit ST for fetching all users, well, you can see that we don't have any property called post
count unless we move further before we can see that property by default.
Mongoose does not include virtual freehold when you convert a document to Jason, so to be able to get
the actual property unless we add additional configuration to that particular model.
So now as a second argument to the schema, that is this one, we come here and then we bring in objects.
So here we pass in what is called to Jason and as an object and you bring in virtuous to true.
So with this one to help us to get this property on the model upon fetching.
So now let's see when we hit st let's look through the user.
Let me collapse the post here and this time around you can see that we have the post count which is
20, but it is nowhere to be found on the user model.
That's a beauty of the virtuous.
So to make it more dynamic instead of this one, we are going to use this.
So this refers to the instance of this particular user.
So if you console.log this, you can see that we are going to see this particular user that we have
found inside our root is this instant.
So now let me comment this one or let me just retain 30 here for the meantime, and let's make the request
and let's check our terminal.
And you can see that we have the entire user and we can access the post and we use the length on it.
So here we go.
Let's remove this one from here and we can make use of this dirt post dirt length.
So moment of truth.
So let's go ahead and then fetch all users and let's collect the post.
And you can see that we have Post County.
If I go ahead and create another post with the same title and description, let's fetch our users and
let's see the post counts and it's from around.
We have three post count how awesome it is so we can do more using the virtual and use this one a lot
in my free word applications.
All right, guys.
So here ends the section about data modeling in Mongoose.
We are going to build a couple of projects to reinforce our skills when it comes to data modeling,
especially when we get to manage that project.