Let's learn about another useful aggregation stage, the $unwind stage. $unwind let's us unwind an array field one creating a new document for every entry where the field value is now each entry. Let's visualize this with an example. If I have the following scheme on the left, title and genres, and unwind on the genres field, I'll get back documents on the right, what? Am I saying that I'm generating a document per each array entry when it was all tight and one embedded, why might this be useful? One example is when we'd like to group on individual entries. In the group lesson, we grouped movies based on their year and we tried to group on year and genres. We would have gotten back many distinct entries because within group, arrays are measured on pure equality, not equivalence. So this array of adventure, action, would not match this array of action adventure. All right, let's use unwind for something real. Let's find the most popular genres by year by 2010 to 2015 within movies collection. I'm going to go ahead and limit this, and say that I'm only considering entries with a run time of 90 minutes or greater. And for popularity, I'll use a value in IMDB.rating. Okay, let's break this down. Here, we'll begin with the match stage ensuring we have an imdb.rating value by specifying that it must be greater than 0. And filtering documents based on year and runtime. Then, we unwind the genres array creating a new document for each entry in the original array. Then we'll group on the year and the now single value genres field and use the average expression to calculate the average rating from imdb.rating. Finally, we sort, first on the year descending, and then the average rating descending. Let's test it out. It's close but not quite there yet. We can see we're getting the most popular genre by year, but we're getting all results back. We just want a single document per year with the highest rated genre. There are many ways to accomplish this, let's look at one of the most simple. Okay, let's examine this new pipeline, it's identical to the previous one with the addition of these two stages. The previous pipeline was returning in the format we wanted there were just too many documents being returned. Here, in this additional group stage we group documents together based on their year. Since they're already sorted in the order we need, we just take the first value we encounter for the genre and the average_rating. Then, we finish with the sort to make sure that they're returned in the order we want. Let's see if it works. Excellent, one document per year with the highest rated genre in that year. Okay, we've seen how unwind works, now there's a few last things to cover. We've been using the short form for unwind, here's the long form for contrast. In the long form, we specify the array we want to unwind by providing a field path expression to the path argument. We can provide a string to include array index. This will create another field in the document with whatever name we specify. With the value set to the index of the element in the original array. Lastly, we can provide a true or false value to preserveNullAndEmptyArrays. True, will create an entry with an empty array with a value specified in the path is either null, missing, or an empty array. One more thing of note. If the documents in our collection are very large and we need to use unwind, we may exceed the default memory limit of the aggregation framework. As always, naturally retain only the information needed with project. And remember we can specifying to allow disk use, and that covers unwind. We've learned a lot. Let's recap on on a few things. Unwind only works on an array of values. There are two forms for unwind, the short form and long form. Using unwind on large collections with big documents may lead to performing issues.