1
00:00:08,850 --> 00:00:10,290
In this video.

2
00:00:10,320 --> 00:00:15,720
Let's talk about another important property or concept.

3
00:00:15,720 --> 00:00:20,130
When it comes to mongoose, it's what we call virtuous.

4
00:00:20,250 --> 00:00:28,350
So in Mongoose, a virtual property is a property that is not stored into the MongoDB.

5
00:00:28,680 --> 00:00:38,550
But instead, we use the virtual house to do what is called computation or add more logic to our document.

6
00:00:38,580 --> 00:00:40,560
So let me show you what I mean here.

7
00:00:40,650 --> 00:00:42,820
So back to the model.

8
00:00:42,840 --> 00:00:45,900
Let's open the user model.

9
00:00:46,380 --> 00:00:53,190
Let's take it that we want to find the number of posts a user has created.

10
00:00:53,280 --> 00:01:02,460
We can do it inside the root that is open, fetching, and then we can fetch a post by a user.

11
00:01:02,610 --> 00:01:10,710
And on that we can use the array method on it to get the total post created by a particular user.

12
00:01:10,920 --> 00:01:15,970
But we can achieve the same concept using what is called virtuous.

13
00:01:15,990 --> 00:01:18,780
So let me show you what I mean.

14
00:01:18,810 --> 00:01:33,660
So now let me collapse this one and let's come down here and let's say find the total post created by

15
00:01:34,410 --> 00:01:37,420
this user.

16
00:01:37,440 --> 00:01:45,680
So here we bring in the schema that is a user schema and we add what is called virtual grade.

17
00:01:45,690 --> 00:01:51,840
So this property is not going to be persisted into the document and save it into DB.

18
00:01:51,870 --> 00:01:56,970
But instead, upon fetching or querying, we can get back that property.

19
00:01:57,120 --> 00:02:07,290
So for this, let's call this one as post count and we add what is called dot gate.

20
00:02:07,680 --> 00:02:11,250
And for this we pass in a callback function.

21
00:02:11,250 --> 00:02:18,470
But for this we are going to use normal function because we want to use this keyword inside that.

22
00:02:18,480 --> 00:02:20,430
So here we go.

23
00:02:22,430 --> 00:02:23,000
Great.

24
00:02:23,000 --> 00:02:24,870
So how are you going to get it done?

25
00:02:24,890 --> 00:02:29,540
For the meantime, let's retain some static value, say 20.

26
00:02:29,660 --> 00:02:37,190
So with this one, when I make a request to find or to fetch a user, I will see a property called post

27
00:02:37,190 --> 00:02:39,680
count, which is equal to 20.

28
00:02:39,680 --> 00:02:41,540
So now let's check it out.

29
00:02:41,540 --> 00:02:49,670
When I hit ST for fetching all users, well, you can see that we don't have any property called post

30
00:02:49,670 --> 00:02:56,360
count unless we move further before we can see that property by default.

31
00:02:56,360 --> 00:03:03,980
Mongoose does not include virtual freehold when you convert a document to Jason, so to be able to get

32
00:03:03,980 --> 00:03:10,820
the actual property unless we add additional configuration to that particular model.

33
00:03:10,850 --> 00:03:20,240
So now as a second argument to the schema, that is this one, we come here and then we bring in objects.

34
00:03:20,420 --> 00:03:30,080
So here we pass in what is called to Jason and as an object and you bring in virtuous to true.

35
00:03:30,530 --> 00:03:38,390
So with this one to help us to get this property on the model upon fetching.

36
00:03:38,390 --> 00:03:43,760
So now let's see when we hit st let's look through the user.

37
00:03:43,790 --> 00:03:50,810
Let me collapse the post here and this time around you can see that we have the post count which is

38
00:03:50,810 --> 00:03:56,360
20, but it is nowhere to be found on the user model.

39
00:03:56,390 --> 00:03:59,690
That's a beauty of the virtuous.

40
00:03:59,720 --> 00:04:05,570
So to make it more dynamic instead of this one, we are going to use this.

41
00:04:05,570 --> 00:04:10,520
So this refers to the instance of this particular user.

42
00:04:10,670 --> 00:04:19,010
So if you console.log this, you can see that we are going to see this particular user that we have

43
00:04:19,010 --> 00:04:22,880
found inside our root is this instant.

44
00:04:23,030 --> 00:04:31,130
So now let me comment this one or let me just retain 30 here for the meantime, and let's make the request

45
00:04:31,130 --> 00:04:34,190
and let's check our terminal.

46
00:04:35,440 --> 00:04:44,950
And you can see that we have the entire user and we can access the post and we use the length on it.

47
00:04:45,070 --> 00:04:47,230
So here we go.

48
00:04:47,260 --> 00:04:58,180
Let's remove this one from here and we can make use of this dirt post dirt length.

49
00:04:58,270 --> 00:05:00,310
So moment of truth.

50
00:05:01,110 --> 00:05:07,350
So let's go ahead and then fetch all users and let's collect the post.

51
00:05:07,350 --> 00:05:10,710
And you can see that we have Post County.

52
00:05:10,920 --> 00:05:19,440
If I go ahead and create another post with the same title and description, let's fetch our users and

53
00:05:19,440 --> 00:05:22,980
let's see the post counts and it's from around.

54
00:05:22,980 --> 00:05:32,520
We have three post count how awesome it is so we can do more using the virtual and use this one a lot

55
00:05:32,520 --> 00:05:35,250
in my free word applications.

56
00:05:35,430 --> 00:05:36,090
All right, guys.

57
00:05:36,090 --> 00:05:40,940
So here ends the section about data modeling in Mongoose.

58
00:05:40,950 --> 00:05:46,980
We are going to build a couple of projects to reinforce our skills when it comes to data modeling,

59
00:05:47,010 --> 00:05:50,640
especially when we get to manage that project.

