1
00:00:00,140 --> 00:00:00,530
All right.

2
00:00:00,530 --> 00:00:03,600
And up next, let's work on the monthly applications.

3
00:00:03,620 --> 00:00:09,100
Now, I can tell you right away that this is going to be tougher for a few reasons.

4
00:00:09,110 --> 00:00:13,130
First of all, we're dealing with dates and that's always the case.

5
00:00:13,190 --> 00:00:15,590
And also we will group.

6
00:00:16,219 --> 00:00:22,820
Based on year and month, and that also adds extra complexity.

7
00:00:22,850 --> 00:00:27,530
So for starters, let's navigate again back to job controller.

8
00:00:27,860 --> 00:00:29,330
I believe for now.

9
00:00:29,330 --> 00:00:30,080
You know what?

10
00:00:30,350 --> 00:00:36,200
I'm just going to comment out so we can set up everything from scratch, but I'll keep it still here

11
00:00:36,200 --> 00:00:39,860
in the job controller just in case I want to showcase something.

12
00:00:39,860 --> 00:00:41,660
So let me start from scratch.

13
00:00:41,660 --> 00:00:44,990
I'm going to go here with Let and Monthly.

14
00:00:45,730 --> 00:00:46,720
Applications.

15
00:00:47,710 --> 00:00:49,780
So this is going to be equal to our weight.

16
00:00:49,780 --> 00:00:52,420
Again, we have the same job.

17
00:00:53,260 --> 00:00:56,590
So basically our model then aggregate.

18
00:00:57,160 --> 00:00:58,390
That's the method.

19
00:00:58,420 --> 00:01:00,870
Then we pass in the array.

20
00:01:00,880 --> 00:01:01,780
And you know what?

21
00:01:02,700 --> 00:01:05,400
The first part is going to be exactly the same.

22
00:01:05,400 --> 00:01:10,650
So again, we're looking for the jobs that belong to the specific user.

23
00:01:10,650 --> 00:01:17,280
And therefore, in the first stage we go with match, then created by and again, we're looking for

24
00:01:17,280 --> 00:01:19,260
the same user ID.

25
00:01:19,500 --> 00:01:27,600
Then in the second one in the group stage, well now it's going to be way more difficult in my opinion.

26
00:01:27,600 --> 00:01:35,100
So we're going to go here with group and then we will actually group, like I said, year and month.

27
00:01:35,100 --> 00:01:38,280
Now, first of all, where the values are located.

28
00:01:38,280 --> 00:01:42,030
Well, they are in the createdat, correct.

29
00:01:42,030 --> 00:01:45,180
So in here we want to set up an object.

30
00:01:45,270 --> 00:01:53,400
In the object, there's going to be an ID, But since we're looking for two values for the year and

31
00:01:53,400 --> 00:01:59,010
a month, we want to set it not equal to whatever we add over here to the property.

32
00:01:59,040 --> 00:02:01,710
We actually want to set it equal to an object.

33
00:02:01,710 --> 00:02:04,150
And this object is going to have two properties.

34
00:02:04,150 --> 00:02:05,590
One is going to be year.

35
00:02:05,770 --> 00:02:09,550
And in order to calculate, we're going to go with.

36
00:02:10,229 --> 00:02:14,290
Dollar sign year and we'll set it equal to created at.

37
00:02:14,310 --> 00:02:21,080
So this is again a special way how in the mongo we can just pull out the year from the date.

38
00:02:21,090 --> 00:02:25,950
So in here we're looking for dollar sign and we're going to go with Createdat.

39
00:02:25,950 --> 00:02:31,500
So dollar sign created at So this is for year.

40
00:02:31,530 --> 00:02:34,400
Then we also want to do the same thing for the month.

41
00:02:34,410 --> 00:02:36,300
So this is totally up to you.

42
00:02:36,300 --> 00:02:39,240
But in my case I think I'm just going to copy.

43
00:02:39,240 --> 00:02:43,830
And what we want to change over here is the month.

44
00:02:44,010 --> 00:02:50,400
So instead of looking for the year now we're looking for a month in the same date.

45
00:02:50,430 --> 00:02:54,870
Please keep that in mind and same deal I want to sum.

46
00:02:55,140 --> 00:02:59,910
So right after the ID still within the same object.

47
00:02:59,910 --> 00:03:01,440
Let's go over here with count.

48
00:03:02,310 --> 00:03:08,970
And then we're going to go with dollar sign and let's set it equal to some.

49
00:03:09,710 --> 00:03:12,230
And one just like we had before.

50
00:03:12,260 --> 00:03:18,140
So let's make a request and let's see what we're going to get back as far as the response and check

51
00:03:18,140 --> 00:03:18,740
it out.

52
00:03:19,190 --> 00:03:20,960
So notice here monthly applications.

53
00:03:20,960 --> 00:03:22,040
That's my array.

54
00:03:23,000 --> 00:03:25,100
And we are moving in the right direction.

55
00:03:25,100 --> 00:03:30,220
So at the moment we have this underscore ID and that is equal to year.

56
00:03:30,230 --> 00:03:34,730
So whichever year it is and then of course the month as well.

57
00:03:34,900 --> 00:03:41,820
And notice how we nicely see how many applications actually reference the month and the year.

58
00:03:41,840 --> 00:03:48,740
So notice how they are combined, because we need to keep in mind that of course there's 12 months in

59
00:03:48,740 --> 00:03:49,460
the year.

60
00:03:49,670 --> 00:03:54,440
So if we'll just randomly say, Hey, get me the month, Well, which year is it?

61
00:03:54,440 --> 00:03:56,960
2022, Is it 2023?

62
00:03:56,960 --> 00:03:59,150
Notice So month seven.

63
00:03:59,240 --> 00:03:59,950
Correct.

64
00:03:59,960 --> 00:04:00,650
So.

65
00:04:01,240 --> 00:04:06,010
If I don't have the year, then it's basically going to give me random months.

66
00:04:06,220 --> 00:04:06,870
All right.

67
00:04:06,880 --> 00:04:08,800
Now let's sort.

68
00:04:09,180 --> 00:04:10,540
And I actually want to.

69
00:04:11,320 --> 00:04:13,870
Get the latest values first.

70
00:04:13,870 --> 00:04:15,190
So how we can do that?

71
00:04:15,190 --> 00:04:21,339
Well, let's navigate back over here to a job controller right after a group.

72
00:04:21,459 --> 00:04:23,170
We'll set up another one.

73
00:04:23,470 --> 00:04:27,220
So my bad over here, we want to set up another stage.

74
00:04:27,220 --> 00:04:28,750
This one will be sort.

75
00:04:29,800 --> 00:04:35,950
And basically we want to go with underscore dot here and underscore.

76
00:04:37,010 --> 00:04:41,690
That month, and both of them set it equal to negative one over here.

77
00:04:41,690 --> 00:04:43,870
So we'll set it equal to an object.

78
00:04:43,880 --> 00:04:53,690
Then we'll go here with the quotation mark, underscore ID here, and let's set it equal to minus one.

79
00:04:54,500 --> 00:04:58,450
So we'll start with basically the biggest value over here.

80
00:04:58,460 --> 00:05:01,880
And then the same deal goes for the month.

81
00:05:02,030 --> 00:05:07,250
So let's set up underscore ID then month.

82
00:05:08,050 --> 00:05:11,410
And this one will be equal again to minus one.

83
00:05:11,560 --> 00:05:13,270
And I want to limit to six.

84
00:05:13,270 --> 00:05:13,930
But you know what?

85
00:05:13,930 --> 00:05:17,110
Let me just make the request first and now check it out.

86
00:05:17,110 --> 00:05:18,600
We nicely sorted.

87
00:05:18,610 --> 00:05:21,790
So now we're getting the latest month.

88
00:05:21,790 --> 00:05:29,070
First, the last step we want to do in the aggregation pipeline is to set up limit of six.

89
00:05:29,080 --> 00:05:34,120
And in order to do that, we just type here limit and we set it equal to six.

90
00:05:34,240 --> 00:05:35,620
So let me run it.

91
00:05:35,620 --> 00:05:37,840
I'm going to go with show stats and check it out.

92
00:05:37,870 --> 00:05:42,880
Now, I'm only going to have six values over here, basically six objects.

93
00:05:43,090 --> 00:05:49,360
Now we're almost almost done, but we still need to run map because take a look at the front end.

94
00:05:49,600 --> 00:05:56,590
Essentially, I want to nicely display which month it is instead of just some random numbers.

95
00:05:56,590 --> 00:06:00,910
And the way we're going to achieve that, we're going to navigate back to job controller.

96
00:06:00,910 --> 00:06:04,800
And again, this is the end result you're looking for.

97
00:06:04,810 --> 00:06:11,410
So at the end, once you're done with the functionality, the monthly applications should be an array

98
00:06:11,410 --> 00:06:19,600
with six objects and then each object should have date and count and date should have following format.

99
00:06:19,690 --> 00:06:25,810
And if you want to treat this as a challenge, just stop the video and resume once you're ready to compare

100
00:06:25,810 --> 00:06:26,950
the results.

101
00:06:27,310 --> 00:06:33,910
I think I'll leave the comment for now and effectively we're just going to go with monthly applications.

102
00:06:34,610 --> 00:06:39,800
We are going to set it equal to monthly applications and then map.

103
00:06:39,890 --> 00:06:41,840
So we're mapping over.

104
00:06:43,150 --> 00:06:47,800
And as far as the callback function, I'm going to call this item.

105
00:06:47,830 --> 00:06:52,930
Now, if I take a look at show stats, each item is an object.

106
00:06:53,140 --> 00:06:53,890
Correct.

107
00:06:54,040 --> 00:06:59,170
And then in there I have ID, which is an object as well as the count.

108
00:06:59,200 --> 00:07:00,830
So same deal.

109
00:07:00,850 --> 00:07:03,790
I want to pull out year and month.

110
00:07:04,310 --> 00:07:06,890
And I also want to pull out the count.

111
00:07:06,920 --> 00:07:08,540
So let's navigate back.

112
00:07:09,110 --> 00:07:12,170
And let's set up the destructuring syntax.

113
00:07:12,170 --> 00:07:19,940
So I'm going to say, you know, get me the ID, but since I know that ID or underscore ID is an object.

114
00:07:20,680 --> 00:07:22,900
I'm going to pull out both of them right away.

115
00:07:22,900 --> 00:07:26,350
And also I'll pull out the count.

116
00:07:26,380 --> 00:07:30,640
Now, all of that is coming from where it's coming from my item.

117
00:07:30,940 --> 00:07:38,570
And once I have this in place, now I want to use my deejays basically to format the date.

118
00:07:38,590 --> 00:07:43,360
So essentially, when it comes to count, I'll nicely return in the object.

119
00:07:43,360 --> 00:07:45,040
So this is not going to change.

120
00:07:45,040 --> 00:07:48,130
What I want to change is the year and a month.

121
00:07:48,250 --> 00:07:54,280
So I want to return one property, which is going to be date property and I want to nicely format.

122
00:07:54,310 --> 00:07:58,300
Now, as far as the formatting, we're going to go here with date.

123
00:07:58,450 --> 00:08:00,020
We'll set it equal to day.

124
00:08:00,550 --> 00:08:02,770
Basically we can right away invoke it.

125
00:08:02,810 --> 00:08:04,540
Then we want to go with month.

126
00:08:04,570 --> 00:08:11,380
And as far as the value we want to go with minus one since in day DJs they start from zero.

127
00:08:11,380 --> 00:08:14,380
But as far as MongoDB, January is actually one.

128
00:08:14,500 --> 00:08:17,290
Therefore, we go here with this minus one.

129
00:08:17,410 --> 00:08:20,500
Then year will pass in as it is.

130
00:08:20,500 --> 00:08:22,500
Year is equal to year.

131
00:08:22,510 --> 00:08:25,810
And then let's nicely call format.

132
00:08:25,930 --> 00:08:28,840
And let's just come up with the format we want to use.

133
00:08:28,840 --> 00:08:30,490
And in my case, I'm going to go with this one.

134
00:08:30,490 --> 00:08:34,690
So three characters for a month and two characters for the year.

135
00:08:34,720 --> 00:08:43,510
Now at the very, very end, we also want to reverse it because like I said, I want to return the latest

136
00:08:43,510 --> 00:08:45,410
month last one.

137
00:08:45,430 --> 00:08:48,010
So when we were sorting.

138
00:08:48,750 --> 00:08:55,530
We actually returned the last month as a first one because we wanted to set the limit, correct?

139
00:08:55,560 --> 00:08:58,230
I only wanted the last six months.

140
00:08:58,260 --> 00:09:00,780
Now when we are returning this to.

141
00:09:01,660 --> 00:09:02,530
A front end.

142
00:09:02,560 --> 00:09:03,780
I want to reverse it.

143
00:09:03,790 --> 00:09:05,920
So for starters, let's pass in date.

144
00:09:05,920 --> 00:09:08,920
So these are the two properties we're returning.

145
00:09:09,100 --> 00:09:12,790
And let's also run a reverse on the map.

146
00:09:12,820 --> 00:09:13,750
Let's save it.

147
00:09:13,750 --> 00:09:16,360
I believe I can just remove this.

148
00:09:16,360 --> 00:09:17,010
Or you know what?

149
00:09:17,020 --> 00:09:20,020
Actually, let's make the request first.

150
00:09:20,730 --> 00:09:23,850
Let's see whether everything works and then we'll remove.

151
00:09:23,880 --> 00:09:26,040
So now let me navigate back to show stats.

152
00:09:26,070 --> 00:09:30,090
Let me make the request and notice it looks like everything is correct.

153
00:09:30,090 --> 00:09:35,810
So latest month is over here and date is also nicely formatted.

154
00:09:35,820 --> 00:09:42,750
So now of course we can just remove this example code and we can start working on the front end.

