$cmp

$cmp演算子は、指定された 2 つの値を比較します。 $cmp演算子は、最初の値が 2 番目の値より小さい場合は -1 を返し、2 つの値が等しい場合は 0、最初の値が 2 番目の値より大きい場合は 1 を返します。

構文

{
  $cmp: [<firstValueToCompare>, <secondValueToCompare>]
}

パラメーター

パラメーター Description
<firstValueToCompare> $cmp演算子によって 2 番目の値と比較される最初の値
<secondValueToCompare> $cmp演算子によって比較される 2 番目の値

例示

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

例 1 - 店舗の売上合計を 25,000 ドルと比較する

ボルダー イノベーションカンパニー内の店舗の売上合計を 25,000 ドルと比較するには、まずクエリを実行して店舗の会社名をフィルター処理します。 次に、$cmpを使用して sales.totalSales フィールドを 25000 と比較します。 最後に、結果の店舗の名前と総売上のみをプロジェクトします。

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["Boulder Innovations"]
        }
    }
}, {
    $project: {
        name: 1,
        "sales.salesByCategory.totalSales": 1,
        greaterThan25000: {
            $cmp: ["$sales.revenue", 25000]
        }
    }
}])

このクエリによって返される最初の 2 つの結果は次のとおりです。

[
    {
        "_id": "a5040801-d127-4950-a320-e55f6aed4b36",
        "name": "Boulder Innovations | DJ Equipment Pantry - West Christopher",
        "sales": {
            "salesByCategory": [
                {
                    "totalSales": 21522
                }
            ]
        },
        "greaterThan25000": -1
    },
    {
        "_id": "bb6e097a-e204-4b64-9f13-5ae8426fcc76",
        "name": "Boulder Innovations | Kitchen Appliance Outlet - Lake Chazville",
        "sales": {
            "salesByCategory": [
                {
                    "totalSales": 24062
                },
                {
                    "totalSales": 24815
                }
            ]
        },
        "greaterThan25000": 1
    }
]