- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $unwind (aggregation)
$unwind (aggregation)¶
On this page
Definition¶
-
$unwind¶ Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
Syntax¶
You can pass a field path operand or a document operand to unwind an array field.
Field Path Operand¶
You can pass the array field path to $unwind. When using
this syntax, $unwind does not output a document if the field
value is null, missing, or an empty array.
When you specify the field path, prefix the
field name with a dollar sign $ and enclose in quotes.
Document Operand with Options¶
You can pass a document to $unwind to specify various
behavior options.
| Field | Type | Description |
|---|---|---|
| path | string | Field path to an array field. To specify a field path, prefix
the field name with a dollar sign |
| includeArrayIndex | string | Optional. The name of a new field to hold the array index of the
element. The name cannot start with a dollar sign |
| preserveNullAndEmptyArrays | boolean | Optional.
The default value is |
Behaviors¶
Non-Array Field Path¶
Changed in version 3.2: $unwind stage no longer errors on non-array operands. If
the operand does not resolve to an array but is not missing, null,
or an empty array, $unwind treats the operand as a
single element array. If the operand is null, missing, or an empty
array, the behavior of $unwind depends on the value of
the preserveNullAndEmptyArrays
option.
Previously, if a value in the field specified by the field path is
not an array, db.collection.aggregate() generates an
error.
Missing Field¶
If you specify a path for a field that does not exist in an input
document or the field is an empty array, $unwind, by
default, ignores the input document and will not output documents for
that input document.
To output documents where the array field is missing, null or an empty array, use the preserveNullAndEmptyArrays option.
Examples¶
Unwind Array¶
In mongosh, create a sample collection named
inventory with the following document:
The following aggregation uses the $unwind stage to output
a document for each element in the sizes array:
The operation returns the following results:
Each document is identical to the input document except for the value
of the sizes field which now holds a value from the original
sizes array.
Missing or Non-array Values¶
Consider the clothing collection:
$unwind treats the sizes field as a single element
array if:
- the field is present,
- the value is not null, and
- the value is not an empty array.
Expand the sizes arrays with $unwind:
The $unwind operation returns:
- In document
"_id": 1,sizesis a populated array.$unwindreturns a document for each element in thesizesfield. - In document
"_id": 3,sizesresolves to a single element array. - Documents
"_id": 2, "_id": 4, and"_id": 5do not return anything because thesizesfield cannot be reduced to a single element array.
Note
The { path: <FIELD> } syntax is optional. The following
$unwind operations are equivalent.
preserveNullAndEmptyArrays and includeArrayIndex¶
The preserveNullAndEmptyArrays and includeArrayIndex examples use the following collection:
preserveNullAndEmptyArrays¶
The following $unwind operation uses the
preserveNullAndEmptyArrays
option to include documents whose sizes field is null, missing,
or an empty array.
The output includes those documents where the sizes field is
null, missing, or an empty array:
includeArrayIndex¶
The following $unwind operation uses the
includeArrayIndex option to include
the array index in the output.
The operation unwinds the sizes array and includes the array index
in the new arrayIndex field. If the sizes field does not
resolve to a populated array but is not missing, null, or an empty
array, the arrayIndex field is null.
Group by Unwound Values¶
In mongosh, create a sample collection named
inventory2 with the following documents:
The following pipeline unwinds the sizes array and groups the
resulting documents by the unwound size values:
- First Stage:
The
$unwindstage outputs a new document for each element in thesizesarray. The stage uses the preserveNullAndEmptyArrays option to include in the output those documents wheresizesfield is missing, null or an empty array. This stage passes the following documents to the next stage:- Second Stage:
The
$groupstage groups the documents bysizesand calculates the average price of each size. This stage passes the following documents to the next stage:- Third Stage:
The
$sortstage sorts the documents byaveragePricein descending order. The operation returns the following result:
Unwind Embedded Arrays¶
In mongosh, create a sample collection named
sales with the following documents:
The following operation groups the items sold by their tags and calculates the total sales amount per each tag.
- First Stage
The first
$unwindstage outputs a new document for each element in theitemsarray:- Second Stage
The second
$unwindstage outputs a new document for each element in theitems.tagsarrays:- Third Stage
The
$groupstage groups the documents by the tag and calculates the total sales amount of items with each tag: