对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$zip(表达式操作符)

$zip

转置输入数组的数组,使输出数组的第一个元素将是一个数组,其中包含第一个输入数组的第一个元素、第二个输入数组的第一个元素等。

For example, $zip would transform [ [ 1, 2, 3 ], [ "a", "b", "c" ] ] into [ [ 1, "a" ], [ 2, "b" ], [ 3, "c" ] ].

$zip 通过以下语法实现:

{
$zip: {
inputs: [ <expression1>, <expression2>, ... ],
useLongestLength: <boolean>,
defaults: <array expression>
}
}
操作数
说明

inputs

解析为数组的表达式数组。这些输入数组的元素组合起来形成输出数组的数组。

If any of the inputs arrays resolves to a value of null or refers to a missing field, $zip returns null.

If any of the inputs arrays does not resolve to an array or null nor refers to a missing field, $zip returns an error.

useLongestLength

一个布尔值,用于指定最长数组的长度是否决定输出数组中的数组数量。

默认值为 false:最短数组的长度决定输出数组中数组的数量。

defaults

An expression that resolves to an array of default element values to use if the input arrays have different lengths. You must specify useLongestLength: true along with this field, or else $zip will return an error.

If useLongestLength: true but defaults is empty or not specified, $zip uses null as the default value.

If specifying a non-empty defaults, you must specify a default for each input array or else $zip will return an error.

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.

例子
结果
{ $zip: { inputs: [ [ "a" ], [ "b" ], [ "c" ] ] }
[ [ "a", "b", "c" ] ]
{ $zip: { inputs: [ [ "a" ], [ "b", "c" ] ] } }
[ [ "a", "b" ] ]
{
$zip: {
inputs: [ [ 1 ], [ 2, 3 ] ],
useLongestLength: true
}
}
[ [ 1, 2 ], [ null, 3 ] ]
{
$zip: {
inputs: [ [ 1 ], [ 2, 3 ], [ 4 ] ],
useLongestLength: true,
defaults: [ "a", "b", "c" ]
}
}

因为 useLongestLength: true,$zip 将用相应的 defaults 元素填充较短的输入数组。

这将产生 [ [ 1, 2, 4 ], [ "a", 3, "c" ] ]。

名为 matrices 的集合包含以下文档:

db.matrices.insertMany([
{ matrix: [[1, 2], [2, 3], [3, 4]] },
{ matrix: [[8, 7], [7, 6], [5, 4]] },
])

要计算该集合中每个 3x2 矩阵的转置,您可以使用以下聚合操作:

db.matrices.aggregate([{
$project: {
_id: false,
transposed: {
$zip: {
inputs: [
{ $arrayElemAt: [ "$matrix", 0 ] },
{ $arrayElemAt: [ "$matrix", 1 ] },
{ $arrayElemAt: [ "$matrix", 2 ] },
]
}
}
}
}])

这将返回以下 2x3 矩阵:

{ "transposed" : [ [ 1, 2, 3 ], [ 2, 3, 4 ] ] }
{ "transposed" : [ [ 8, 7, 5 ], [ 7, 6, 4 ] ] }

您可以将 $zip 与 $filter 用于获取数组中元素的子集,将原始索引与每个保留的元素一起保存。

名为 pages 的集合包含以下文档:

db.pages.insertOne( {
"category": "unix",
"pages": [
{ "title": "awk for beginners", reviews: 5 },
{ "title": "sed for newbies", reviews: 0 },
{ "title": "grep made simple", reviews: 2 },
] } )

以下聚合管道将首先压缩 pages 数组的元素及其索引,然后仅筛选出至少包含一条评论的页面:

db.pages.aggregate([{
$project: {
_id: false,
pages: {
$filter: {
input: {
$zip: {
inputs: [ "$pages", { $range: [0, { $size: "$pages" }] } ]
}
},
as: "pageWithIndex",
cond: {
$let: {
vars: {
page: { $arrayElemAt: [ "$$pageWithIndex", 0 ] }
},
in: { $gte: [ "$$page.reviews", 1 ] }
}
}
}
}
}
}])

这将返回以下文档:

{
"pages" : [
[ { "title" : "awk for beginners", "reviews" : 5 }, 0 ],
[ { "title" : "grep made simple", "reviews" : 2 }, 2 ] ]
}
给本页内容打分

在此页面上