1 00:00:02,150 --> 00:00:09,010 We already had a look at a couple of pipeline stages and operators, now I want to dive into some stages 2 00:00:09,020 --> 00:00:15,170 in operators that help you with working with arrays and therefore, I prepared some new data which you 3 00:00:15,170 --> 00:00:23,090 can either import with mongo import or you just go into your mongo shell, create a new collection on 4 00:00:23,090 --> 00:00:23,650 the fly, 5 00:00:23,660 --> 00:00:29,110 I'll name it friends and I'll insert many documents and then add an opening parentheses, 6 00:00:29,150 --> 00:00:30,960 no closing parentheses, 7 00:00:31,010 --> 00:00:34,730 copy in the values and then close it and hit enter 8 00:00:34,880 --> 00:00:37,910 and this should now also add the three elements. 9 00:00:37,910 --> 00:00:47,240 So in the friends collection, I now have these three documents, three persons with name, age and hobbies 10 00:00:47,480 --> 00:00:49,380 and then some exam scores. 11 00:00:49,550 --> 00:00:56,060 And I just need some arrays here because you can do quite a lot of things with arrays in the aggregation 12 00:00:56,060 --> 00:00:57,370 framework. 13 00:00:57,380 --> 00:01:01,080 Now what can we do with these arrays? 14 00:01:01,580 --> 00:01:09,260 Well one thing you often want to do is that you want to kind of merge or combine arrays in a grouping 15 00:01:09,260 --> 00:01:10,040 stage, 16 00:01:10,040 --> 00:01:12,330 so let's start with that. 17 00:01:12,330 --> 00:01:21,100 So for that, I prepared a brand new aggregation pipeline and we'll start with grouping our results. 18 00:01:21,120 --> 00:01:28,860 I will group by age, let's say and by the way, you can also group if you just group by one field, like this 19 00:01:28,950 --> 00:01:34,450 but I'm a fan of the document syntax because there you can assign your own names and make it clearer 20 00:01:34,470 --> 00:01:35,690 by what you grouped. 21 00:01:35,940 --> 00:01:40,890 So I will group by age and of course I do have an age on my documents, 22 00:01:40,890 --> 00:01:48,030 I only got three, so I will only have, well not that many groups but still let's do that. Let's group by age 23 00:01:48,300 --> 00:01:56,040 and let's then say we want to combine the hobby arrays, so that we know which hobbies exist for this age group 24 00:01:56,130 --> 00:02:03,300 and for that age group. And we can do that, we can add an array, we can name it hobbies or you give it 25 00:02:03,300 --> 00:02:07,680 a brand new name, like all hobbies, whatever you want 26 00:02:08,100 --> 00:02:15,450 and now there are two operators mainly that help you with combining array values because you have to 27 00:02:15,450 --> 00:02:20,040 keep in mind that in the group stage, multiple documents are merged into one, 28 00:02:20,040 --> 00:02:29,520 so we all see the way of handling multiple array values in one array. And we can do that here with the 29 00:02:29,970 --> 00:02:41,070 push operator, the push operator allows you to push a new element into the all hobbies array for every 30 00:02:41,910 --> 00:02:43,770 incoming document. 31 00:02:45,360 --> 00:02:50,220 Now every incoming document here will have a hobbies array, 32 00:02:50,400 --> 00:02:57,350 this one, this hobbies field and you could push any field value into the array, not just existing arrays, 33 00:02:57,360 --> 00:03:02,930 so what I will do is I will now push $hobbies. 34 00:03:02,940 --> 00:03:04,180 Now let's give this a try, 35 00:03:04,290 --> 00:03:07,940 let's take this aggregation pipeline and run it, 36 00:03:08,020 --> 00:03:11,750 however not on persons but on friends, 37 00:03:11,760 --> 00:03:14,940 so let's rerun it, like this. 38 00:03:14,940 --> 00:03:23,880 Now what you see is that we have two groups, age 30 and age 29 and all hobbies is an array of arrays because 39 00:03:24,420 --> 00:03:31,870 we push hobbies into our array and hobbies happens to be an array itself, here 40 00:03:32,020 --> 00:03:37,140 hobbies is an array and we just push that value into our all hobbies array here. 41 00:03:37,590 --> 00:03:44,670 That is what I meant, you can push any value of course, not just existing arrays but what if you wanted 42 00:03:44,680 --> 00:03:53,790 to push the values of existing arrays in there but not as one array but you want to pull these values 43 00:03:53,850 --> 00:03:56,460 out of this array and then add them to all hobbies. 44 00:03:56,790 --> 00:04:01,080 Well that is also something you can do and I will show you how it works in the next lecture.