Aracılığıyla paylaş


$zip

$zip işleci, öğe açısından iki veya daha fazla diziyi tek bir dizi dizisiyle birleştirmek için kullanılır. Birden çok diziden ilgili öğeleri tek bir dizi yapısında birleştirmek istediğinizde kullanışlıdır.

Sözdizimi

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

Parametreler

Parametre Description
inputs Öğe açısından birleştirilecek dizi dizisi.
useLongestLength True olarak ayarlanırsa giriş dizilerinin en uzun uzunluğunu kullanan boole değeri. False ise veya belirtilmezse, en kısa uzunluğu kullanır.
defaults True ise ve herhangi bir giriş dizisi en uzun diziden daha kısaysa useLongestLength kullanılacak varsayılan değerler dizisi.

Örnekler

Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.

{
    "_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
                }
            ]
        }
    ]
}

Örnek 1: Temel Kullanım

dizideki categoryNametotalSales ve salesByCategory alanlarını birleştirmek istediğinizi varsayalım. Bu sorgu, alan altındaki categoryWithSales dizi dizilerini tek tek döndürür. useLongestLength true ayarı aşağıdaki çıkışı döndürürken değeri false diziyi Napkins çıkıştan kaldırır.

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
                }
            }
        }
    }
])

Bu sorgu aşağıdaki sonucu döndürür.

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