運算子 $not 會在指定的運算式上執行邏輯 NOT 運算,並選取不符合表達式的檔。
語法
{
field: {
$not: {
< operator - expression >
}
}
}
參數
| 參數 | Description |
|---|---|
operator-expression |
要否定的運算式 |
範例
請參考商店集合中的此範例檔。
{
"_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:使用 NOT 作業作為邏輯查詢運算符
此查詢會使用具有 $eq 的運算子擷取 $not 全職員工人數不等於 5 的商店。 它只會 name 傳回 最多兩份這類相符檔的 和 staff 欄位。
db.stores.find({
"staff.totalStaff.fullTime": {
$not: {
$eq: 5
}
}
}, {
"name": 1,
"staff": 1
}).limit(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:使用 NOT 運算子作為布爾運算式來識別非大量存放區
此查詢會擷取銷售量不高 (不超過 50,000) 的商店。
db.stores.aggregate([
{
$project: {
name: 1,
totalSales: "$sales.salesByCategory.totalSales",
isNotHighVolume: {
$not: { $gt: ["$sales.salesByCategory.totalSales", 50000] }
},
storeCategory: {
$cond: [
{ $not: { $gt: ["$sales.salesByCategory.totalSales", 50000] } },
"High Volume Store",
"Small/Medium Store"
]
}
}
},
{ $limit: 2 }
])
此查詢傳回的前兩個結果是:
[
{
"_id": "905d1939-e03a-413e-a9c4-221f74055aac",
"name": "Trey Research | Home Office Depot - Lake Freeda",
"totalSales": [ 37978 ],
"isNotHighVolume": false,
"storeCategory": "Small/Medium Store"
},
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"totalSales": [ 25731 ],
"isNotHighVolume": false,
"storeCategory": "Small/Medium Store"
}
]
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。