In this exercise, you’ll add a stage to the aggregation pipeline that uses the $unwind
aggregate operator to deconstruct the interests
array in the sales
collection.
To unwind the array
- On the Pipeline tab of the Aggregation Editor, right-click the
$match
pipeline stage, and then click Add New Stage After Selected Stage. - On the new tab, select the $unwind option from the Operator drop-down list. The $unwind operator creates a document for each element in the specified array field.
- In the editor window, replace the placeholder text and curly braces with the following code:
{ path: "$interests" }
The $unwind operator requires the path argument, which specifies the field path to the target array. In this case, the target array is the interests field (preceded by a dollar sign).
- Run the Stage Input and Stage Output tabs. The following figure shows part of the results, as they appear in Table View.
Because the $unwind
operator creates a document for every array element in each input document, the output now includes 105 documents, rather than 37. For example, the input data includes a single document with an email value of [email protected]
, but the output data includes four documents with an email value of [email protected]
because the original document contained four elements in the interests
array.
- Click Save on the Aggregation Editor toolbar to save your changes.
- Leave the Aggregation tab open and the existing statement in place for the next exercise.