Not bad, not bad.
We have our data in a database, so now let's start working on the show stats functionality, and we'll
start doing that on the backend, on the server and effectively.
Before we go any further, let me just showcase what we're shooting for.
So in a database, we have jobs collection and each job belongs to a certain user.
And here's what I want to do.
I want to group those jobs for that specific user.
So not all of the jobs we're looking for specific jobs that belong to John, Peter or Suzanne or whatever.
And I want to group them based on the status.
So whether they're spending, whether it's an interview or the job has been declined and the way we'll
do that is using the aggregation pipeline since this is somewhat of an advanced topic.
I'll do everything step by step.
And I'm not sure whether our console.log as much on a server, but I'll definitely be sending back data
to the postman just so I don't have to switch the screen.
So yes, it might take a little bit or set everything up.
And if you want to find out more, here's an awesome link back to the MongoDB docks, where you can
essentially get more info for your own project.
And our functionality is going to look something like this where in the jobs controller, where we have
show stats instead of that silly string, we're going to go with job model and notice how we're calling
this aggregate.
So that's the method name that pretty much every model has, and the syntax looks like this where in
the method we pass in the array and then we just set up the steps.
So think of aggregation pipeline as series of steps.
So first, I want to get all the jobs that belong to a certain user.
Then I want to group them in some way.
So in our case, based on a start, and then we can sort them and that type of thing.
And when it comes to the syntax, we go here with Volition Match.
So that is going to be our first step, the match one.
Then we need to set it equal to an object and then we need to choose the property that we'll base our
match.
And in our case, that is going to be created by.
So what I'm trying to get.
Is all the jobs created by that specific user?
Now remember, we already have the user I.D. because we are using the middleware, the gotcha here is
that it is still a string.
And if this is the case where we need to get that object I.D. for this functionality to work, so in
this line, what we're saying is get me all the jobs that belong to Peter, Susan and whatever and the
next video or worry about the.
So let's start working on that.
And first, we want to go to jobs comptroller.
And yes, we'll have to get the Mango one because in order to convert the string back through the Mongoose
model, we need to go with mongoose types and then object.
And that's why we need to import it first.
So I'm going to go write off 30 check permissions.
We're going to go here with Mongoose.
And that is coming from the package.
So Mongoose package, then let's keep on moving.
We're looking for get start somewhere here.
So right after the delete one, I guess the function name is show that.
So I just go with that one and then I'm going to set it up as let and you'll see why.
Because we'll make some modifications.
So let's go.
Let's start 08.
So this is a synchronous still job.
And then, like I said, aggregate.
That's the method name.
And here we pass in the array with our steps.
And each step is going to be represented as an object.
And on the first one is the match one.
So now I want to match the jobs that I'm looking for.
Like I said, we're going to go with created and keep in mind that quickly you can use different properties,
but you just need to make sense.
In our case, I want to match all the jobs that belong to a certain user.
And like I said, we do have access to Iraq user and user ID because of the middleware, but we need
to turn this one into a object.
So Mongoose types that and that object that lets passing the Iraq user and the user ID and if everything
is correct.
Once we send back the stats, we should see something different than what we currently have, which
is our sewage stream, so let's go here with the rest that status.
And we're looking for a status codes van OK.
And Jason.
And then let's bask in the stats.
We're still sending in the object.
Now I'm sending the stats property.
So now let me go to the post man and let me make sure that I've logged in as John and everything.
So yeah, I still have the valid token.
And now let's go to the show stats and let's send it.
And now we have this massive array.
So that means that everything works now if you want.
You can send back the length and all that.
But remember that we will be structuring our data anyway differently.
So it's not going to be necessary at the moment.
We're pretty much getting all of the jobs.
And if you are a job, let's say with beta, you'll notice that that job is not going to be in disarray.
Now I'm not going to do that during the videos, but just letting you know that, yes, all of these
jobs belong to you, John.
So we got the entire data.
Now we need to figure out how to group it.