Freigeben über


$concatArrays

Der $concatArrays Operator wird verwendet, um mehrere Arrays in einem einzigen Array zu kombinieren. Dieser Operator ist nützlich, wenn Sie Arrays aus verschiedenen Dokumenten oder Feldern in einem Dokument zusammenführen müssen.

Syntax

{
  $concatArrays: ["<array1>", "<array2>"]
}

Die Parameter

Parameter Description
<array1>, <array2> Die Arrayfelder, die für die Verkettung vorgesehen sind.

Examples

Betrachten Sie dieses Beispieldokument aus der Stores-Sammlung.

{
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
    "location": {
        "lat": 60.1441,
        "lon": -141.5012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 2,
            "partTime": 0
        }
    },
    "sales": {
        "salesByCategory": [
            {
                "categoryName": "DJ Headphones",
                "totalSales": 35921
            },
            {
                "categoryName": "DJ Cables",
                "totalSales": 1000
            }
        ],
        "fullSales": 3700
    },
    "promotionEvents": [
        {
            "eventName": "Bargain Blitz Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 3,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 2,
                    "Day": 18
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Turntables",
                    "discountPercentage": 18
                },
                {
                    "categoryName": "DJ Mixers",
                    "discountPercentage": 15
                }
            ]
        },
        {
            "eventName": "Discount Delight Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 18
                }
            }
        }
    ],
    "tag": [
        "#ShopLocal",
        "#FashionStore",
        "#SeasonalSale",
        "#FreeShipping",
        "#MembershipDeals"
    ]
}

Beispiel 1: Verketten von Arrays in einem Dokument

Diese Abfrage führt das categoryName Feld aus dem promotionEvents.discounts Array mit dem tag Array in ein einzelnes combinedTags-Array zusammen.

db.stores.aggregate([{
    $match: {
        _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
    }
}, {
    $project: {
        combinedTags: {
            $concatArrays: ["$promotionEvents.discounts.categoryName", "$tag"]
        }
    }
}])

Diese Abfrage gibt das folgende Ergebnis zurück.

[
  {
      "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
      "combinedTags": [ '#ShopLocal', '#NewArrival', '#NewArrival', '#FreeShipping' ]
  }
]