All right.
And up next, let's work on the monthly applications.
Now, I can tell you right away that this is going to be tougher for a few reasons.
First of all, we're dealing with dates and that's always the case.
And also we will group.
Based on year and month, and that also adds extra complexity.
So for starters, let's navigate again back to job controller.
I believe for now.
You know what?
I'm just going to comment out so we can set up everything from scratch, but I'll keep it still here
in the job controller just in case I want to showcase something.
So let me start from scratch.
I'm going to go here with Let and Monthly.
Applications.
So this is going to be equal to our weight.
Again, we have the same job.
So basically our model then aggregate.
That's the method.
Then we pass in the array.
And you know what?
The first part is going to be exactly the same.
So again, we're looking for the jobs that belong to the specific user.
And therefore, in the first stage we go with match, then created by and again, we're looking for
the same user ID.
Then in the second one in the group stage, well now it's going to be way more difficult in my opinion.
So we're going to go here with group and then we will actually group, like I said, year and month.
Now, first of all, where the values are located.
Well, they are in the createdat, correct.
So in here we want to set up an object.
In the object, there's going to be an ID, But since we're looking for two values for the year and
a month, we want to set it not equal to whatever we add over here to the property.
We actually want to set it equal to an object.
And this object is going to have two properties.
One is going to be year.
And in order to calculate, we're going to go with.
Dollar sign year and we'll set it equal to created at.
So this is again a special way how in the mongo we can just pull out the year from the date.
So in here we're looking for dollar sign and we're going to go with Createdat.
So dollar sign created at So this is for year.
Then we also want to do the same thing for the month.
So this is totally up to you.
But in my case I think I'm just going to copy.
And what we want to change over here is the month.
So instead of looking for the year now we're looking for a month in the same date.
Please keep that in mind and same deal I want to sum.
So right after the ID still within the same object.
Let's go over here with count.
And then we're going to go with dollar sign and let's set it equal to some.
And one just like we had before.
So let's make a request and let's see what we're going to get back as far as the response and check
it out.
So notice here monthly applications.
That's my array.
And we are moving in the right direction.
So at the moment we have this underscore ID and that is equal to year.
So whichever year it is and then of course the month as well.
And notice how we nicely see how many applications actually reference the month and the year.
So notice how they are combined, because we need to keep in mind that of course there's 12 months in
the year.
So if we'll just randomly say, Hey, get me the month, Well, which year is it?
2022, Is it 2023?
Notice So month seven.
Correct.
So.
If I don't have the year, then it's basically going to give me random months.
All right.
Now let's sort.
And I actually want to.
Get the latest values first.
So how we can do that?
Well, let's navigate back over here to a job controller right after a group.
We'll set up another one.
So my bad over here, we want to set up another stage.
This one will be sort.
And basically we want to go with underscore dot here and underscore.
That month, and both of them set it equal to negative one over here.
So we'll set it equal to an object.
Then we'll go here with the quotation mark, underscore ID here, and let's set it equal to minus one.
So we'll start with basically the biggest value over here.
And then the same deal goes for the month.
So let's set up underscore ID then month.
And this one will be equal again to minus one.
And I want to limit to six.
But you know what?
Let me just make the request first and now check it out.
We nicely sorted.
So now we're getting the latest month.
First, the last step we want to do in the aggregation pipeline is to set up limit of six.
And in order to do that, we just type here limit and we set it equal to six.
So let me run it.
I'm going to go with show stats and check it out.
Now, I'm only going to have six values over here, basically six objects.
Now we're almost almost done, but we still need to run map because take a look at the front end.
Essentially, I want to nicely display which month it is instead of just some random numbers.
And the way we're going to achieve that, we're going to navigate back to job controller.
And again, this is the end result you're looking for.
So at the end, once you're done with the functionality, the monthly applications should be an array
with six objects and then each object should have date and count and date should have following format.
And if you want to treat this as a challenge, just stop the video and resume once you're ready to compare
the results.
I think I'll leave the comment for now and effectively we're just going to go with monthly applications.
We are going to set it equal to monthly applications and then map.
So we're mapping over.
And as far as the callback function, I'm going to call this item.
Now, if I take a look at show stats, each item is an object.
Correct.
And then in there I have ID, which is an object as well as the count.
So same deal.
I want to pull out year and month.
And I also want to pull out the count.
So let's navigate back.
And let's set up the destructuring syntax.
So I'm going to say, you know, get me the ID, but since I know that ID or underscore ID is an object.
I'm going to pull out both of them right away.
And also I'll pull out the count.
Now, all of that is coming from where it's coming from my item.
And once I have this in place, now I want to use my deejays basically to format the date.
So essentially, when it comes to count, I'll nicely return in the object.
So this is not going to change.
What I want to change is the year and a month.
So I want to return one property, which is going to be date property and I want to nicely format.
Now, as far as the formatting, we're going to go here with date.
We'll set it equal to day.
Basically we can right away invoke it.
Then we want to go with month.
And as far as the value we want to go with minus one since in day DJs they start from zero.
But as far as MongoDB, January is actually one.
Therefore, we go here with this minus one.
Then year will pass in as it is.
Year is equal to year.
And then let's nicely call format.
And let's just come up with the format we want to use.
And in my case, I'm going to go with this one.
So three characters for a month and two characters for the year.
Now at the very, very end, we also want to reverse it because like I said, I want to return the latest
month last one.
So when we were sorting.
We actually returned the last month as a first one because we wanted to set the limit, correct?
I only wanted the last six months.
Now when we are returning this to.
A front end.
I want to reverse it.
So for starters, let's pass in date.
So these are the two properties we're returning.
And let's also run a reverse on the map.
Let's save it.
I believe I can just remove this.
Or you know what?
Actually, let's make the request first.
Let's see whether everything works and then we'll remove.
So now let me navigate back to show stats.
Let me make the request and notice it looks like everything is correct.
So latest month is over here and date is also nicely formatted.
So now of course we can just remove this example code and we can start working on the front end.