運算子 $nor 會在表示式陣列上執行邏輯 NOR 運算,並選取所有指定表示式失敗的檔。
語法
{
$nor: [{
< expression1 >
}, {
< expression2 >
}, ..., {
< expressionN >
}]
}
參數
| 參數 | Description |
|---|---|
expression |
表達式陣列,所有運算式都必須為 false,才能包含檔 |
範例
請參考商店集合中的此範例檔。
{
"_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
})
此查詢傳回的前兩個結果是:
[
{
"_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 作業
若要在 2024 年 9 月尋找沒有銷售超過 $100,000 美元的商店,而不銷售「數字手錶」,或在 2024 年 9 月使用 $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
})
此查詢所傳回的其中一個結果為:
[
{
"_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
}
]
}
]
}
]
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。