$spínač

Operátor $switch slouží k vyhodnocení řady podmínek a vrácení hodnoty na základě první podmínky, která se vyhodnotí jako true. To je užitečné, když potřebujete implementovat složitou podmíněnou logiku v rámci kanálů agregace.

Syntaxe

{
  $switch: {
    branches: [
      { case: <expression>, then: <expression> },
      { case: <expression>, then: <expression> }
    ],
    default: <expression>
  }
}

Parametry

Parameter Description
větve Pole dokumentů, z nichž každý obsahuje
případ Výraz, který se vyhodnotí jako buď true nebo false
potom Výraz, který se má vrátit, pokud se přidružený case výraz vyhodnotí jako true
default Výraz, který se má vrátit, pokud se žádný z case výrazů nevyhodnotí jako true. Toto pole je volitelné.

Examples

Podívejte se na tento ukázkový dokument z kolekce stores.

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

Příklad 1: Určení typu zaměstnanců na základě počtu zaměstnanců na plný úvazek a částečný úvazek

Tento dotaz určuje typ zaměstnanců na základě jejich počtu.

db.stores.aggregate([{
        $project: {
            name: 1,
            staffType: {
                $switch: {
                    branches: [{
                            case: {
                                $eq: ["$staff.totalStaff.partTime", 0]
                            },
                            then: "Only Full time"
                        },
                        {
                            case: {
                                $eq: ["$staff.totalStaff.fullTime", 0]
                            },
                            then: "Only Part time"
                        }
                    ],
                    default: "Both"
                }
            }
        }
    },
    // Limit the result to the first 3 documents
    {
        $limit: 3
    }
])

První tři výsledky vrácené tímto dotazem jsou:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
    "staffType": "Only Full time"
  },
  {
    "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
    "name": "VanArsdel, Ltd. | Musical Instrument Outlet - East Cassie",
    "staffType": "Both"
  },
  {
    "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
    "name": "Northwind Traders | Bed and Bath Place - West Oraland",
    "staffType": "Both"
  }
]