该 $not
运算符对指定的表达式执行逻辑 NOT 运算,并选择与表达式不匹配的文档。
语法
{
field: {
$not: {
< operator - expression >
}
}
}
参数
参数 | DESCRIPTION |
---|---|
operator-expression |
要否定的表达式 |
例子
让我们了解 stores
数据集中的示例 json 的用法。
{
"_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的操作员的全职员工数不等于 5 $not
的商店。 它仅返回最多两个此类匹配文档的 name
字段和 staff
字段。
db.stores.find({
"staff.totalStaff.fullTime": {
$not: {
$eq: 5
}
}
}, {
"name": 1,
"staff": 1
}).limit(2)
查询返回最多两个存储,其中全职员工计数不同于 5。
[
{
"_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 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。