你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用对象: MongoDB vCore
该 $not
运算符对指定的表达式执行逻辑 NOT 运算,并选择与表达式不匹配的文档。
语法
{ field: { $not: { <operator-expression> } } }
参数
参数 | 类型 | DESCRIPTION |
---|---|---|
operator-expression |
表达式 | 要否定的表达式 |
例子
示例 1:基本 NOT作
查找没有完全 5 名全职员工的商店:
db.stores.find({
"staff.totalStaff.fullTime": {
$not: { $eq: 5 }
}
})
输出:
{
eventName: 'Unbeatable Bargain Bash',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 23 },
endDate: { Year: 2024, Month: 7, Day: 2 }
},
discounts: [
{ categoryName: 'Cabinets', discountPercentage: 8 },
{ categoryName: 'Desks', discountPercentage: 22 }
]
}
示例 2:不带正则表达式
查找名称不以“First Up”开头的存储:
db.stores.find({
name: {
$not: /^First Up/
}
})
输出:
{
_id: 'cac30620-fd99-4ee2-8329-c87980ab2b24',
name: 'Contoso, Ltd. | Handbag Bargains - South Jovanny',
location: { lat: 19.6816, lon: 18.6237 },
staff: { totalStaff: { fullTime: 10, partTime: 19 } },
sales: {
totalSales: 56878,
salesByCategory: [
{ categoryName: 'Mini Bags', totalSales: 20543 },
{ categoryName: 'Satchels', totalSales: 36335 }
]
},
promotionEvents: [
{
eventName: 'Price Slash Carnival',
promotionalDates: {
startDate: { Year: 2023, Month: 6, Day: 29 },
endDate: { Year: 2023, Month: 7, Day: 8 }
},
discounts: [
{ categoryName: 'Messenger Bags', discountPercentage: 9 },
{ categoryName: 'Shoulder Bags', discountPercentage: 8 }
]
},
{
eventName: 'Major Bargain Bash',
promotionalDates: {
startDate: { Year: 2023, Month: 9, Day: 27 },
endDate: { Year: 2023, Month: 10, Day: 5 }
},
discounts: [
{ categoryName: 'Hobo Bags', discountPercentage: 15 },
{ categoryName: 'Messenger Bags', discountPercentage: 9 }
]
},
{
eventName: 'Unbeatable Bargain Bash',
promotionalDates: {
startDate: { Year: 2023, Month: 12, Day: 26 },
endDate: { Year: 2024, Month: 1, Day: 4 }
},
discounts: [
{ categoryName: 'Bucket Bags', discountPercentage: 22 },
{ categoryName: 'Shoulder Bags', discountPercentage: 10 }
]
},
{
eventName: 'Big Bargain Blitz',
promotionalDates: {
startDate: { Year: 2024, Month: 3, Day: 25 },
endDate: { Year: 2024, Month: 4, Day: 1 }
},
discounts: [
{ categoryName: 'Crossbody Bags', discountPercentage: 25 },
{ categoryName: 'Backpacks', discountPercentage: 19 }
]
},
{
eventName: 'Clearance Carnival',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 23 },
endDate: { Year: 2024, Month: 6, Day: 30 }
},
discounts: [
{ categoryName: 'Bucket Bags', discountPercentage: 8 },
{ categoryName: 'Mini Bags', discountPercentage: 16 }
]
},
{
eventName: 'Blowout Bargain Bash',
promotionalDates: {
startDate: { Year: 2024, Month: 9, Day: 21 },
endDate: { Year: 2024, Month: 9, Day: 29 }
},
discounts: [
{ categoryName: 'Hobo Bags', discountPercentage: 23 },
{ categoryName: 'Clutches', discountPercentage: 8 }
]
}
]
}
示例 3:复杂 NOT作
查找没有任何促销活动的商店,只有 20% 折扣:
db.stores.find({
"promotionEvents.discounts.discountPercentage": {
$not: { $eq: 20 }
}
})
输出:
{
_id: 'f25b56da-2789-42f2-b844-3c88c7384307',
name: 'Fourth Coffee | Home Decor Corner - Kavonshire',
location: { lat: -82.8806, lon: 125.2905 },
staff: { totalStaff: { fullTime: 19, partTime: 14 } },
sales: {
totalSales: 6485,
salesByCategory: [ { categoryName: 'Picture Frames', totalSales: 6485 } ]
},
promotionEvents: [
{
eventName: 'Crazy Discount Days',
promotionalDates: {
startDate: { Year: 2024, Month: 9, Day: 21 },
endDate: { Year: 2024, Month: 10, Day: 1 }
},
discounts: [
{ categoryName: 'Mirrors', discountPercentage: 21 },
{ categoryName: 'Vases', discountPercentage: 15 }
]
}
]
}..
局限性
$not
仅接受单个表达式- 不能直接包含另一个逻辑运算符