On this page本页内容
$zip¶New in version 3.4.版本3.4中的新功能。
Transposes an array of input arrays so that the first element of the output array would be an array containing, the first element of the first input array, the first element of the second input array, etc.转换输入数组的数组,使输出数组的第一个元素是一个包含第一个输入数组的第一个元素、第二个输入数组的第一个元素等的数组。
For example, 例如,$zip would transform [ [ 1, 2, 3 ], [ "a", "b", "c" ] ] into [ [ 1, "a" ], [ 2, "b" ], [ 3, "c" ] ].$zip会将[ [ 1, 2, 3 ], [ "a", "b", "c" ] ]转换为[ [ 1, "a" ], [ 2, "b" ], [ 3, "c" ] ]。
$zip has the following syntax:语法如下所示:
inputs |
|
useLongestLength |
|
defaults |
|
The input arrays do not need to be of the same length. 输入数组不需要具有相同的长度。By default, the output array has the length of the shortest input array, but the 默认情况下,输出数组的长度与最短输入数组的长度相同,但useLongestLength option instructs $zip to output an array as long as the longest input array.useLongestLength选项指示$zip输出与最长输入数组长度相同的数组。
This yields |
A collection called 名为matrices contains the following documents:matrices的集合包含以下文档:
To compute the transpose of each 3x2 matrix in this collection, you can use the following aggregation operation:要计算此集合中每个3x2矩阵的转置,可以使用以下聚合操作:
This will return the following 2x3 matrices:这将返回以下2x3矩阵:
You can use 您可以使用$zip with $filter to obtain a subset of elements in an array, saving the original index alongside each retained element.$zip和$filter来获取数组中元素的子集,并将原始索引与每个保留的元素一起保存。
A collection called 名为pages contains the following document:pages的集合包含以下文档:
The following aggregation pipeline will first zip the elements of the 下面的聚合管道将首先将pages array together with their index, and then filter out only the pages with at least one review:pages数组的元素及其索引压缩在一起,然后仅筛选出至少有一次审阅的页面:
This will return the following document:这将返回以下文档: