1 00:00:02,140 --> 00:00:07,770 So in the last lecture you saw how you can push elements into newly created arrays and I will emphasize 2 00:00:07,810 --> 00:00:12,930 this again, that the values you push don't have to be arrays themselves. 3 00:00:12,940 --> 00:00:16,990 We could have pushed the age or the name, that would have worked too 4 00:00:17,110 --> 00:00:22,360 and then we would have all hobbies or a different name then which would be an array of all the ages 5 00:00:22,360 --> 00:00:24,890 that are in the group or all the names that are in the group, 6 00:00:24,910 --> 00:00:27,030 so this would have worked too. 7 00:00:27,040 --> 00:00:33,440 I however want to have these values in all hobbies but not in their nested arrays 8 00:00:33,550 --> 00:00:40,600 and for that, I will add a new pipeline stage which we haven't seen before and that is the unwind stage. 9 00:00:41,020 --> 00:00:49,200 The unwind stage is always a great stage when you have an array of which you want to pull out the elements. 10 00:00:49,330 --> 00:00:56,500 Now to understand what unwind does, I'll temporarily move my grouping out of there so that I can just copy 11 00:00:56,500 --> 00:00:59,770 that later and I'll complete the unwind stage. 12 00:00:59,770 --> 00:01:05,500 Unwind has two different syntaxes and you can check the official docs for all the details as always, 13 00:01:06,280 --> 00:01:07,910 in its most common usage, 14 00:01:08,050 --> 00:01:11,800 you just pass the name of a field that holds an array, 15 00:01:12,010 --> 00:01:13,180 so hobbies, 16 00:01:13,180 --> 00:01:20,500 remember we get our own initial documents as an input to this stage and now, we just unwind on hobbies. 17 00:01:20,530 --> 00:01:22,780 Now what does unwind do? 18 00:01:23,140 --> 00:01:27,320 Well let's take that stage and let's see. If I execute this, 19 00:01:27,430 --> 00:01:33,370 you see what I get back are a couple of documents more than we originally had because now we have 20 00:01:33,370 --> 00:01:38,770 a document Max with hobbies sports and we have Max again with hobbies cooking. 21 00:01:38,770 --> 00:01:46,170 So what unwind does is it basically flattens the array by repeating the document 22 00:01:46,250 --> 00:01:54,730 that held the array as often as needed to merge it with the array elements. So the original array of Max 23 00:01:54,970 --> 00:01:57,190 simply has sports and cooking, 24 00:01:57,190 --> 00:01:59,140 therefore Max was repeated twice, 25 00:01:59,170 --> 00:02:05,880 we get two new documents, Max with sports and Max with cooking and the same is true for all other elements. 26 00:02:05,950 --> 00:02:13,990 So where group merges multiple documents into one, unwind takes one document and spits out multiple 27 00:02:13,990 --> 00:02:14,930 documents 28 00:02:15,340 --> 00:02:17,850 and now with that, we can group again 29 00:02:17,890 --> 00:02:22,870 but now we can simply take hobbies which is no longer an array but a single value. 30 00:02:23,200 --> 00:02:27,540 So if I now bring back my group stage, I can leave it as it is 31 00:02:28,220 --> 00:02:32,570 but now you will see, if I repeat this, 32 00:02:32,710 --> 00:02:36,070 I still only have two groups because the ages haven't changed 33 00:02:36,310 --> 00:02:38,150 but now you see all hobbies holds 34 00:02:38,170 --> 00:02:41,710 just my array values and not the embedded arrays. 35 00:02:41,740 --> 00:02:46,240 However you can also see that we can have duplicate values, 36 00:02:46,240 --> 00:02:48,120 so what can we do about that?