$zip

$zip 运算符用于将两个或多个数组元素合并为一个数组。 如果要将多个数组中的相关元素合并到单个数组结构中,这非常有用。

语法

{
  $zip: {
    inputs: [ <array1>, <array2>, ... ],
    useLongestLength: <boolean>, // Optional
    defaults: <array> // Optional
  }
}

参数

参数 说明
inputs 要按元素合并的数组数组。
useLongestLength 一个布尔值,如果设置为 true,则使用输入数组的最长长度。 如果为 false 或未指定,则使用最短长度。
defaults 如果为 useLongestLength true,并且任何输入数组都短于最长数组,则使用默认值数组。

例子

请考虑商店集合中的这个示例文档。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

示例 1:基本用法

假设你想要合并 categoryName 数组中的 totalSalessalesByCategory 字段。 此查询返回字段下的 categoryWithSales 数组的单个数组。 useLongestLength true设置为返回以下输出,而从输出中删除false数组的值Napkins

db.stores.aggregate([{
        $match: {
            _id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6"
        }
    },
    {
        $project: {
            name: 1,
            categoryNames: "$sales.salesByCategory.categoryName",
            totalSales: "$sales.salesByCategory.totalSales",
            categoryWithSales: {
                $zip: {
                    inputs: ["$sales.salesByCategory.categoryName", "$sales.salesByCategory.totalSales"],
                    useLongestLength: false
                }
            }
        }
    }
])

此查询返回以下结果。

[
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "categoryNames": ["Stockings"],
    "totalSales": [25731],
    "categoryWithSales": [["Stockings", 25731]]
  }
]