1 00:00:02,380 --> 00:00:07,670 Now with all the projections we did thus far, let's now add another stage again 2 00:00:07,930 --> 00:00:10,120 after all our projections, 3 00:00:10,210 --> 00:00:18,490 so here I'll add another group stage because this is another common scenario that you first want to restructure 4 00:00:18,490 --> 00:00:19,070 your data, 5 00:00:19,090 --> 00:00:21,960 recalculate some fields as we are doing it, 6 00:00:22,030 --> 00:00:25,300 we're adding brand new fields which didn't exist before 7 00:00:25,600 --> 00:00:30,220 and then you want to group on these derived fields. 8 00:00:30,220 --> 00:00:37,240 In my case here I want to group on the birth date and yes, this was available before but not as a date 9 00:00:37,270 --> 00:00:37,730 at least, 10 00:00:37,750 --> 00:00:41,250 we converted it and I will now take advantage of this conversion. 11 00:00:41,290 --> 00:00:46,810 So I'll take advantage of my birth date which is now stored as a date in the group stage here. 12 00:00:46,960 --> 00:00:52,570 We saw the group stage before, we add _id here and then we define by which value we want to 13 00:00:52,570 --> 00:00:53,550 group. 14 00:00:53,590 --> 00:01:00,550 Now here I want to group by birthdate but not by the date but let's say by the year, so birth year is by 15 00:01:00,550 --> 00:01:02,250 which I want to group, 16 00:01:02,470 --> 00:01:09,160 therefore we add a document here and then the operator is the ISO year operator which basically retrieves 17 00:01:09,400 --> 00:01:12,200 the year out of a date. 18 00:01:12,370 --> 00:01:19,010 So the date then is stored in $birthdate pointing at my birth date field of course 19 00:01:19,120 --> 00:01:23,530 and now what do I want to do when grouping these documents? 20 00:01:23,990 --> 00:01:28,750 Well I want to calculate how many persons were born in this year, 21 00:01:28,750 --> 00:01:35,920 so num persons is what I calculate here and I use sum one to basically add one for every document that 22 00:01:35,920 --> 00:01:36,950 goes into that group 23 00:01:37,000 --> 00:01:42,430 and since one document represents one person, I will have the number of persons per birth year here at the 24 00:01:42,430 --> 00:01:42,950 end. 25 00:01:43,240 --> 00:01:49,300 So now with that, whoops this is ISO week year by the way, should be week year. 26 00:01:49,390 --> 00:01:58,030 So now let's copy that, paste it in here and you see, I got my years extracted here as a long number, 27 00:01:58,060 --> 00:02:00,200 this is what mongodb did for me 28 00:02:00,460 --> 00:02:03,150 and I see how many persons were born in there. 29 00:02:03,460 --> 00:02:09,160 Obviously my other projection phases are now rendered a bit useless since I don't take advantage of all the 30 00:02:09,320 --> 00:02:10,440 other fields I added here. 31 00:02:10,450 --> 00:02:11,790 This is just temporary, 32 00:02:11,800 --> 00:02:16,030 I just wanted to show you that you can group on the results of projections. 33 00:02:16,030 --> 00:02:17,890 So here I have the group stage, 34 00:02:17,950 --> 00:02:29,630 let's now maybe even sort and let's sort on the num persons in descending order to start with the most 35 00:02:29,630 --> 00:02:32,900 persons first. If I now enter this here, 36 00:02:32,900 --> 00:02:38,280 now we see most persons in the example dataset were born in 1955.