$not
The `$not` operator performs a logical NOT operation on a specified expression and selects documents that don't match the expression.
Syntax
{
field: {
$not: {
<operator-expression>
}
}
}
Parameters
operator-expression
objectrequiredThe expression to negate
Examples
Find stores with staff not equal to specific value
This query retrieves stores where the number of full-time staff isn't equal to 5 using the `$not` operator with $eq. It returns only the `name` and `staff` fields for up to two such matching documents.
Query:
db.stores.find({
"staff.totalStaff.fullTime": {
$not: {
$eq: 5
}
}
}, {
"name": 1,
"staff": 1
}).limit(2)
Output:
[
{
"_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
}
}
}
]
Negate comparison condition
This query retrieves stores that don't have high sales volume (not greater than 50,000).
Query:
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 }
])
Output:
[
{
"_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"
}
]