From the course: MongoDB Aggregation Pipeline: Advanced Data Analysis and Manipulation

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Working with arrays and $unwind

Working with arrays and $unwind

- [Instructor] MongoDB natively supports working with documents that have arrays. Working with arrays in MongoDB is fairly intuitive as you can treat them like any other data type. One of the more powerful features of arrays in MongoDB is the ability to unwind them using the dollar sign unwind operator. What this does is that it expands a single document into many, with each document representing one of the items in the array. This is much easier to understand when you actually see it in action. So let's go ahead and create an aggregation that unwinds an array. Our use case for doing this is to figure out which vendors get the most sales. This way we know which vendors are more popular so that we can purchase more of our products from them. In MongoDB Compass, open up the orders collection and navigate to the aggregations tab. If we look at the documents in the orders collection, we'll see that they have a customer…

Contents