1 00:00:02,220 --> 00:00:05,040 So I'm still in that same database 2 00:00:05,250 --> 00:00:09,290 I was in the end of the last video and let's now use that aggregate framework. 3 00:00:09,420 --> 00:00:14,810 We use it in a very simple way, instead of running find on persons or find one, 4 00:00:14,850 --> 00:00:22,070 we now run aggregate and we already saw this one time in this course already with look up in the schemas 5 00:00:22,070 --> 00:00:28,620 and relations module. The aggregate method takes an array and it takes an array because we define 6 00:00:28,650 --> 00:00:32,890 a series of steps that should be run on our data. 7 00:00:32,910 --> 00:00:37,440 The first step will receive the entire data right from the collection you could say 8 00:00:37,770 --> 00:00:42,570 and the next step can then do something with the data returned by the first step and so on. 9 00:00:42,570 --> 00:00:48,390 Now one important piece of information, aggregate does not go ahead, fetch all the data from the database 10 00:00:48,600 --> 00:00:51,780 and then give it to you and then do something on it, 11 00:00:51,840 --> 00:00:57,080 of course the first step runs on the database and can take advantage of indexes, 12 00:00:57,180 --> 00:01:02,140 so if you filter in the first step or you sort there, you can take advantage of your indexes, 13 00:01:02,280 --> 00:01:09,300 so you don't have to fetch all the documents just because you're using aggregate. Aggregate as find executes 14 00:01:09,300 --> 00:01:14,230 on the mongodb server and therefore can take advantages of things like indexes 15 00:01:14,370 --> 00:01:15,380 and it will. 16 00:01:15,780 --> 00:01:22,410 So what could be a first step that we run? Now to make this a bit easier to read, I'll type this in multiple 17 00:01:22,410 --> 00:01:25,820 lines by not closing that expression immediately 18 00:01:26,060 --> 00:01:30,380 and the first step is then a document, every step is a document 19 00:01:30,390 --> 00:01:38,550 I should say. Now the first step here could be a match phase and match essentially is just a filtering step, 20 00:01:38,550 --> 00:01:44,550 so you define some criteria on which you want to filter your data in that persons collection. 21 00:01:44,550 --> 00:01:48,580 Now you can filter here, in the same way you can filter in find, 22 00:01:48,900 --> 00:01:54,300 so all the things you learn there on how you can query documents, how you can match for greater than 23 00:01:54,300 --> 00:01:56,840 values and so on applies here too. 24 00:01:57,180 --> 00:02:03,150 So what we could do here is we could for example and if we have a look at our data structure again, we 25 00:02:03,150 --> 00:02:07,910 could for example look at females only. 26 00:02:08,550 --> 00:02:16,060 So to do that, let's simply add a condition here where we say gender should be equal to female. 27 00:02:16,210 --> 00:02:19,930 Now we can already finish our pipeline at this point if we want to, 28 00:02:20,050 --> 00:02:26,320 we can close the square brackets around our stages and close the parentheses of aggregate and I get 29 00:02:26,320 --> 00:02:27,980 back a cursor here, 30 00:02:28,060 --> 00:02:31,060 you can tell by the fact that I see type it here too. 31 00:02:31,240 --> 00:02:35,580 So just like the find method, the aggregate method returns a cursor, 32 00:02:35,710 --> 00:02:41,180 so you have to use it as you use the cursor returned by the find method in your application. 33 00:02:41,590 --> 00:02:44,590 Besides that, we can also pretty print that 34 00:02:44,830 --> 00:02:50,800 and now you see, well some results at least of the results we fetched and we should only see females 35 00:02:50,830 --> 00:02:51,600 in there. 36 00:02:51,610 --> 00:02:58,030 This was of course only one step, the match step and the match step is not that exciting because there, 37 00:02:58,120 --> 00:03:03,760 we as I said query it exactly the same way we can query in the find method or we filter in the same 38 00:03:03,760 --> 00:03:07,750 way I should say. More interesting is the group stage so 39 00:03:07,940 --> 00:03:10,150 let's take a look at that in the next lecture.