$not
The $not operator is used to negate the result of a given expression. It allows you to select documents where the specified condition does not match.
Planner version 2.0 added index support for $not {eq} and $not {in}.
Parameters
-
expression: The expression to negate.
Example (MongoDB Shell)
The following example demonstrates how to use the $not operator to find documents where the status field is not equal to "active".
Create sample documents
db.users.insertMany([ { name: "John", status: "active" }, { name: "Jane", status: "inactive" }, { name: "Bob", status: "pending" }, { name: "Alice", status: "active" } ]);
Query example
db.users.find({ status: { $not: { $eq: "active" } } });
Output
[
{
_id: ObjectId('...'),
name: 'Jane',
status: 'inactive'
},
{
_id: ObjectId('...'),
name: 'Bob',
status: 'pending'
}
]
Code examples
To view a code example for using the $not command, choose the tab for the language that you want to use: