From the course: MongoDB Essential Training

Unlock the full course today

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

$unwind

$unwind

- [Instructor] If you have a document with an array you can use the $unwind stage to create one output document for each array element. Let's try that within the terminal. Again, we are going to be using the inventory collection, so let's just remind ourselves what that looks like. Okay, cool. And here we are going to unwind the variations array so that we can get a document for each car variation. Okay, the way we are going to do that is by first specifying the collection on which we're going to run the aggregation. Then we'll use the aggregate method. And inside it, you know it, there's going to be an array, with a document for the $unwind stage. And to the $unwind stage, we are going to pass a string, which contains the variations field name. And to that we are going to have to add a dollar sign at the beginning. Okay, let's try this. Awesome. We now have individual documents for each car variation. That's often very…

Contents