On this page本页内容
$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.每个输出文档都是输入文档,数组字段的值由元素替换。
You can pass a field path operand or a document operand to unwind an array field.可以传递字段路径操作数或文档操作数以展开数组字段。
You can pass the array field path to 您可以将数组字段路径传递给$unwind
. $unwind
。When using this syntax, 使用此语法时,如果字段值为$unwind
does not output a document if the field value is null, missing, or an empty array.null
、缺失或为空数组,则$unwind
不会输出文档。
When you specify the field path, prefix the field name with a dollar sign 指定字段路径时,请在字段名称前面加上美元符号$
and enclose in quotes.$
,并用引号括起来。
New in version 3.2.版本3.2中的新功能。
You can pass a document to 您可以向$unwind
to specify various behavior options.$unwind
传递文档以指定各种行为选项。
path | string |
|
includeArrayIndex | string |
|
preserveNullAndEmptyArrays | boolean |
|
Changed in version 3.2.在版本3.2中更改。$unwind
stage no longer errors on non-array operands. $unwind
阶段在非数组操作数上不再出现错误。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. null
或为空数组,$unwind
将操作数视为单元素数组。If the operand is null, missing, or an empty array, the behavior of 如果操作数为$unwind
depends on the value of the preserveNullAndEmptyArrays option.null
、缺少或为空数组,$unwind
的行为取决于PreserveNullAndEmptyArray
选项的值。
Previously, if a value in the field specified by the field path is not an array, 以前,如果字段路径指定的字段中的值不是数组,db.collection.aggregate()
generates an error.db.collection.aggregate()
将生成错误。
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.$unwind
将忽略该输入文档,并且不会输出该输入文档的文档。
New in version 3.2:To output documents where the array field is missing, null or an empty array, use the preserveNullAndEmptyArrays option.若要输出缺少数组字段、空数组或空数组的文档,请使用PreserveNullAndEmptyArray
选项。
From the 在mongo
shell, create a sample collection named inventory
with the following document:mongo
shell中,使用以下文档创建名为inventory
的示例集合:
The following aggregation uses the 以下聚合使用$unwind
stage to output a document for each element in the sizes
array:$unwind
阶段为sizes
数组中的每个元素输出文档:
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.sizes
字段的值除外,该字段现在保存原始sizes
数组中的值。
includeArrayIndex
preserveNullAndEmptyArrays
¶New in version 3.2.版本3.2中的新功能。
From the 从mongo
shell, create a sample collection named inventory2
with the following documents:mongo
shell创建名为inventory2的样本集合,其中包含以下文档:
The following 以下$unwind
operations are equivalent and return a document for each element in the sizes
field. $unwind
操作是等效的,并为sizes
字段中的每个元素返回一个文档。If the 如果sizes
field does not resolve to an array but is not missing, null, or an empty array, $unwind
treats the non-array operand as a single element array.sizes
字段未解析为数组,但未丢失、为null
或为空数组,$unwind
将非数组操作数视为单元素数组。
The operation returns the following documents:该操作将返回以下文档:
includeArrayIndex
¶The following 下面的$unwind
operation uses the includeArrayIndex option to include the array index in the output.$unwind
操作使用includeArrayIndex
选项将数组索引包括在输出中。
The operation unwinds the 该操作展开sizes
array and includes the array index of the array index in the new arrayIndex
field. sizes
数组,并将数组索引的数组索引包含在新增的arrayIndex
字段中。If the 如果sizes
field does not resolve to an array but is not missing, null, or an empty array, the arrayIndex
field is null
.sizes
字段未解析为数组,但未丢失、为null
或为空数组,则arrayIndex
字段为null
。
preserveNullAndEmptyArrays
¶The following 下面的$unwind
operation uses the preserveNullAndEmptyArrays option to include documents whose sizes
field is null, missing, or an empty array.$unwind
操作使用preserveNullAndEmptyArrays
选项包括sizes
字段为null
、缺少或空数组的文档。
The output includes those documents where the 输出包括sizes
field is null, missing, or an empty array:sizes
字段为null
、缺少或空数组的文档:
From the 从mongo
shell, create a sample collection named inventory2
with the following documents:mongo
shell创建名为inventory2的样本集合,其中包含以下文档:
The following pipeline unwinds the 以下管道展开sizes
array and groups the resulting documents by the unwound size values:sizes
数组,并根据展开的大小值对结果文档进行分组:
The $unwind
stage outputs a new document for each element in the sizes
array. $unwind
阶段为sizes
数组中的每个元素输出一个新文档。The stage uses the preserveNullAndEmptyArrays option to include in the output those documents where 该阶段使用sizes
field is missing, null or an empty array. preserveNullAndEmptyArrays
选项在输出中包括缺少sizes
字段、null
或空数组的文档。This stage passes the following documents to the next stage:此阶段将以下文档传递到下一阶段:
The $group
stage groups the documents by sizes
and calculates the average price of each size. $group
阶段按sizes
对文档进行分组,并计算每个大小的平均价格。This stage passes the following documents to the next stage:此阶段将以下文档传递到下一阶段:
The $sort
stage sorts the documents by averagePrice
in descending order. $sort
阶段按averagePrice
降序对文档进行排序。The operation returns the following result:该操作返回以下结果:
From the 从mongo
shell, create a sample collection named sales
with the following documents:mongo
shell中,创建一个名为sales
的样本集合,其中包含以下文档:
The following operation groups the items sold by their tags and calculates the total sales amount per each tag.以下操作按标签对销售的商品进行分组,并计算每个标签的总销售额。
The first 第一个$unwind
stage outputs a new document for each element in the items
array:$unwind
阶段为items
数组中的每个元素输出一个新文档:
The second 第二个$unwind
stage outputs a new document for each element in the items.tags
arrays:$unwind
阶段为items.tags
数组中的每个元素输出一个新文档:
The $group
stage groups the documents by the tag and calculates the total sales amount of items with each tag:$group
阶段按标记对文档进行分组,并计算每个标记的项目总销售额: