適用於:
MongoDB 虛擬核心
運算子 $nor
會在表示式陣列上執行邏輯 NOR 運算,並選取所有指定表示式失敗的檔。
語法
{ $nor: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
參數
參數 | 類型 | 說明 |
---|---|---|
expression |
陣列 | 表達式陣列,所有運算式都必須為 false,才能包含檔 |
範例
範例 1:基本 NOR 作業
尋找沒有超過 15 名全職員工或超過 20 名兼職員工的商店:
db.stores.find({
$nor: [
{ "staff.totalStaff.fullTime": { $gt: 15 } },
{ "staff.totalStaff.partTime": { $gt: 20 } }
]
})
這會產生下列輸出:
{
eventName: 'Steal the Show Sale',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 23 },
endDate: { Year: 2024, Month: 7, Day: 1 }
},
discounts: [
{ categoryName: 'Charms', discountPercentage: 10 },
{ categoryName: 'Bracelets', discountPercentage: 13 }
]
}
範例 2:複雜 NOR 作業
尋找沒有下列任何條件的商店:2024 年 9 月總銷售額超過 100000、銷售「數字手錶」或促銷:
db.stores.find({
$nor: [
{ "sales.totalSales": { $gt: 100000 } },
{ "sales.salesByCategory.categoryName": "Digital Watches" },
{
"promotionEvents": {
$elemMatch: {
"promotionalDates.startDate.Month": 9,
"promotionalDates.startDate.Year": 2024
}
}
}
]
})
這會產生下列輸出:
{
_id: 'binary-test',
name: 'Test Store',
logo: Binary(Buffer.from("627566666572", "hex"), 0),
signature: Binary(Buffer.from("74657374", "hex"), 0)
}
範例 3:具有多個字段條件的 NOR
尋找不符合上述任何位置和員工準則的存放區:
db.stores.find({
$nor: [
{ "location.lat": { $gt: 0 } },
{ "staff.totalStaff.fullTime": { $lt: 10 } },
{ "sales.totalSales": { $gt: 50000 } }
]
})
這會產生下列輸出:
{
_id: 'db5051a8-17d1-4b01-aa2f-31e64623e5ac',
name: 'First Up Consultants | Watch Mart - Port Zack',
location: { lat: -62.6354, lon: 46.2917 },
staff: { totalStaff: { fullTime: 12, partTime: 9 } },
sales: {
totalSales: 6118,
salesByCategory: [ { categoryName: 'Digital Watches', totalSales: 6118 } ]
},
promotionEvents: [
{
eventName: 'Incredible Bargain Blitz',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 23 },
endDate: { Year: 2024, Month: 6, Day: 30 }
},
discounts: [
{
categoryName: 'Chronograph Watches',
discountPercentage: 13
},
{ categoryName: 'Diving Watches', discountPercentage: 21 }
]
},
{
eventName: 'Fantastic Deals Festival',
promotionalDates: {
startDate: { Year: 2024, Month: 9, Day: 21 },
endDate: { Year: 2024, Month: 9, Day: 29 }
},
discounts: [
{ categoryName: 'Digital Watches', discountPercentage: 25 },
{ categoryName: 'Pilot Watches', discountPercentage: 17 }
]
}
]
}