1
00:00:00,240 --> 00:00:01,080
Not bad, not bad.

2
00:00:01,350 --> 00:00:08,100
We have our data in a database, so now let's start working on the show stats functionality, and we'll

3
00:00:08,100 --> 00:00:12,930
start doing that on the backend, on the server and effectively.

4
00:00:12,930 --> 00:00:16,230
Before we go any further, let me just showcase what we're shooting for.

5
00:00:16,830 --> 00:00:23,280
So in a database, we have jobs collection and each job belongs to a certain user.

6
00:00:23,880 --> 00:00:25,080
And here's what I want to do.

7
00:00:26,090 --> 00:00:29,960
I want to group those jobs for that specific user.

8
00:00:30,350 --> 00:00:36,170
So not all of the jobs we're looking for specific jobs that belong to John, Peter or Suzanne or whatever.

9
00:00:36,830 --> 00:00:39,830
And I want to group them based on the status.

10
00:00:40,190 --> 00:00:47,270
So whether they're spending, whether it's an interview or the job has been declined and the way we'll

11
00:00:47,270 --> 00:00:54,710
do that is using the aggregation pipeline since this is somewhat of an advanced topic.

12
00:00:55,100 --> 00:00:56,720
I'll do everything step by step.

13
00:00:57,730 --> 00:01:04,030
And I'm not sure whether our console.log as much on a server, but I'll definitely be sending back data

14
00:01:04,030 --> 00:01:06,790
to the postman just so I don't have to switch the screen.

15
00:01:06,820 --> 00:01:10,990
So yes, it might take a little bit or set everything up.

16
00:01:11,420 --> 00:01:17,830
And if you want to find out more, here's an awesome link back to the MongoDB docks, where you can

17
00:01:18,460 --> 00:01:23,130
essentially get more info for your own project.

18
00:01:23,770 --> 00:01:29,560
And our functionality is going to look something like this where in the jobs controller, where we have

19
00:01:29,890 --> 00:01:37,090
show stats instead of that silly string, we're going to go with job model and notice how we're calling

20
00:01:37,090 --> 00:01:38,020
this aggregate.

21
00:01:38,440 --> 00:01:45,040
So that's the method name that pretty much every model has, and the syntax looks like this where in

22
00:01:45,040 --> 00:01:49,790
the method we pass in the array and then we just set up the steps.

23
00:01:49,830 --> 00:01:54,070
So think of aggregation pipeline as series of steps.

24
00:01:54,250 --> 00:01:58,780
So first, I want to get all the jobs that belong to a certain user.

25
00:01:59,200 --> 00:02:02,170
Then I want to group them in some way.

26
00:02:02,230 --> 00:02:07,270
So in our case, based on a start, and then we can sort them and that type of thing.

27
00:02:07,900 --> 00:02:12,190
And when it comes to the syntax, we go here with Volition Match.

28
00:02:12,610 --> 00:02:16,540
So that is going to be our first step, the match one.

29
00:02:16,810 --> 00:02:23,740
Then we need to set it equal to an object and then we need to choose the property that we'll base our

30
00:02:23,740 --> 00:02:24,190
match.

31
00:02:24,880 --> 00:02:26,980
And in our case, that is going to be created by.

32
00:02:27,430 --> 00:02:29,290
So what I'm trying to get.

33
00:02:30,190 --> 00:02:33,460
Is all the jobs created by that specific user?

34
00:02:33,970 --> 00:02:40,030
Now remember, we already have the user I.D. because we are using the middleware, the gotcha here is

35
00:02:40,030 --> 00:02:42,070
that it is still a string.

36
00:02:43,040 --> 00:02:49,370
And if this is the case where we need to get that object I.D. for this functionality to work, so in

37
00:02:49,370 --> 00:02:57,380
this line, what we're saying is get me all the jobs that belong to Peter, Susan and whatever and the

38
00:02:57,390 --> 00:02:58,820
next video or worry about the.

39
00:02:59,870 --> 00:03:01,610
So let's start working on that.

40
00:03:01,720 --> 00:03:05,150
And first, we want to go to jobs comptroller.

41
00:03:05,150 --> 00:03:12,200
And yes, we'll have to get the Mango one because in order to convert the string back through the Mongoose

42
00:03:12,200 --> 00:03:15,650
model, we need to go with mongoose types and then object.

43
00:03:15,860 --> 00:03:17,750
And that's why we need to import it first.

44
00:03:18,290 --> 00:03:20,720
So I'm going to go write off 30 check permissions.

45
00:03:21,560 --> 00:03:23,060
We're going to go here with Mongoose.

46
00:03:23,480 --> 00:03:25,700
And that is coming from the package.

47
00:03:26,150 --> 00:03:29,510
So Mongoose package, then let's keep on moving.

48
00:03:29,960 --> 00:03:32,900
We're looking for get start somewhere here.

49
00:03:33,650 --> 00:03:38,870
So right after the delete one, I guess the function name is show that.

50
00:03:38,870 --> 00:03:45,020
So I just go with that one and then I'm going to set it up as let and you'll see why.

51
00:03:45,110 --> 00:03:46,730
Because we'll make some modifications.

52
00:03:46,730 --> 00:03:47,420
So let's go.

53
00:03:47,570 --> 00:03:49,610
Let's start 08.

54
00:03:49,640 --> 00:03:52,100
So this is a synchronous still job.

55
00:03:52,100 --> 00:03:53,780
And then, like I said, aggregate.

56
00:03:54,110 --> 00:03:55,040
That's the method name.

57
00:03:55,340 --> 00:03:58,700
And here we pass in the array with our steps.

58
00:03:59,030 --> 00:04:02,180
And each step is going to be represented as an object.

59
00:04:02,450 --> 00:04:05,330
And on the first one is the match one.

60
00:04:05,410 --> 00:04:09,020
So now I want to match the jobs that I'm looking for.

61
00:04:09,350 --> 00:04:14,300
Like I said, we're going to go with created and keep in mind that quickly you can use different properties,

62
00:04:14,780 --> 00:04:16,399
but you just need to make sense.

63
00:04:16,550 --> 00:04:22,250
In our case, I want to match all the jobs that belong to a certain user.

64
00:04:22,670 --> 00:04:28,760
And like I said, we do have access to Iraq user and user ID because of the middleware, but we need

65
00:04:28,760 --> 00:04:31,370
to turn this one into a object.

66
00:04:32,030 --> 00:04:41,690
So Mongoose types that and that object that lets passing the Iraq user and the user ID and if everything

67
00:04:41,690 --> 00:04:42,290
is correct.

68
00:04:43,360 --> 00:04:49,810
Once we send back the stats, we should see something different than what we currently have, which

69
00:04:49,810 --> 00:04:52,750
is our sewage stream, so let's go here with the rest that status.

70
00:04:53,410 --> 00:04:56,530
And we're looking for a status codes van OK.

71
00:04:57,070 --> 00:04:58,360
And Jason.

72
00:04:58,630 --> 00:05:00,670
And then let's bask in the stats.

73
00:05:00,970 --> 00:05:02,530
We're still sending in the object.

74
00:05:02,980 --> 00:05:05,260
Now I'm sending the stats property.

75
00:05:05,560 --> 00:05:10,600
So now let me go to the post man and let me make sure that I've logged in as John and everything.

76
00:05:10,960 --> 00:05:12,850
So yeah, I still have the valid token.

77
00:05:13,240 --> 00:05:17,110
And now let's go to the show stats and let's send it.

78
00:05:17,410 --> 00:05:19,630
And now we have this massive array.

79
00:05:19,810 --> 00:05:23,080
So that means that everything works now if you want.

80
00:05:23,500 --> 00:05:26,260
You can send back the length and all that.

81
00:05:26,260 --> 00:05:31,060
But remember that we will be structuring our data anyway differently.

82
00:05:31,510 --> 00:05:33,960
So it's not going to be necessary at the moment.

83
00:05:34,000 --> 00:05:37,180
We're pretty much getting all of the jobs.

84
00:05:37,750 --> 00:05:44,460
And if you are a job, let's say with beta, you'll notice that that job is not going to be in disarray.

85
00:05:44,500 --> 00:05:49,900
Now I'm not going to do that during the videos, but just letting you know that, yes, all of these

86
00:05:49,900 --> 00:05:52,270
jobs belong to you, John.

87
00:05:52,780 --> 00:05:54,730
So we got the entire data.

88
00:05:55,030 --> 00:05:57,550
Now we need to figure out how to group it.

