次の方法で共有


$nor

$nor演算子は、式の配列に対して論理 NOR 演算を実行し、指定されたすべての式に失敗するドキュメントを選択します。

構文

{
    $nor: [{
        < expression1 >
    }, {
        < expression2 >
    }, ..., {
        < expressionN >
    }]
}

パラメーター

パラメーター Description
expression 式の配列。ドキュメントを含める場合は、すべて false にする必要があります

例示

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: 基本的な NOR 操作

15 人を超えるフルタイムスタッフも 20 人を超えるパートタイム スタッフもいないストアを検索するには、両方の条件に対して $nor 演算子を使用してクエリを実行します。 次に、結果セット内のストアの名前フィールドとスタッフ フィールドのみをプロジェクトします。

db.stores.find({
    $nor: [{
        "staff.totalStaff.fullTime": {
            $gt: 15
        }
    }, {
        "staff.totalStaff.partTime": {
            $gt: 20
        }
    }]
}, {
    "name": 1,
    "staff": 1
})

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

[
    {
        "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
        "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
        "staff": {
            "totalStaff": {
                "fullTime": 9,
                "partTime": 18
            }
        }
    },
    {
        "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
        "name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
        "staff": {
            "totalStaff": {
                "fullTime": 7,
                "partTime": 6
            }
        }
    }
]

例 2: 複雑な NOR 操作

売上が 100,000 ドルを超えない店舗、"Digital Watches" の売上がない店舗、または 2024 年 9 月のプロモーションがない店舗を検索するには、3 つの条件すべてに対して $nor 演算子を使用してクエリを実行します。 最後に、結果セット内のストアからの名前、販売、およびプロモーション イベントのみをプロジェクトします。

db.stores.find({
    $nor: [{
        "sales.totalSales": {
            $gt: 100000
        }
    }, {
        "sales.salesByCategory.categoryName": "Digital Watches"
    }, {
        "promotionEvents": {
            $elemMatch: {
                "promotionalDates.startDate.Month": 9,
                "promotionalDates.startDate.Year": 2024
            }
        }
    }]
}, {
    "name": 1,
    "sales": 1,
    "promotionEvents": 1
})

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

[
    {
        "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
        "sales": {
            "salesByCategory": [
                {
                    "categoryName": "DJ Headphones",
                    "totalSales": 35921
                }
            ],
            "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
                    }
                ]
            }
        ]
    }
]