MongoDB NOT operator ( $not )
In MongoDB, the $not
operator is a logical query operator that allows us to negate or reverse the condition specified in a query. It plays a crucial role in filtering documents by excluding those that match a given expression. This powerful operator is often used with other comparison operators such as $gt
, $eq
, $in
, $ne
, etc., to refine search results and perform more complex queries.
This article will explain the use of the $not
operator in MongoDB with clear explanations, examples, and best practices to help us leverage it effectively in our database operations.
What is the MongoDB $not
Operator?
The $not
operator in MongoDB is used to negate or reverse the logic of another query expression. It returns documents where the value of a field does not match the specified condition. This operator is often used when we want to exclude documents that meet a particular condition or pattern.
- Negates Conditions: The
$not
operator reverses the effect of another query expression. For example, if we're looking for values greater than a certain number,$not
will retrieve values that are less than or equal to that number. - Works with Other Operators: We can use
$not
with other operators like$gt
,$eq
,$in
,$ne
, etc., to negate specific conditions. - Includes Documents without the Field: The
$not
operator will also return documents where the field is missing, in addition to those that don’t match the condition. - Regular Expressions:
$not
can be used with regular expressions to exclude documents that match a given pattern.
Syntax:
{ field: { $not: { operator-expression } } }
Key Terms
field:
name of the field we want to check.operator-expression:
operator expression that defines the condition to be negated.
Examples of Using $not
Operator in MongoDB
In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: contributor
- Document: three documents that contain the details of the contributors in the form of field-value pairs.

1. Matching values using $not
operator
In this example, we are retrieving only those employee’s documents whose salary is not greater than 2000.
Query:
db.contributor.find({salary: {$not: {$gt: 2000}}}).pretty()
Output

Explanation: This query will return all documents where the salary is less than or equal to 2000.
Example 2: Matching values in nested/embedded documents using $not
operator:
In this example, we are retrieving only those employee’s documents whose age is not equal to 24.
Query:
db.contributor.find({"personal.age": {$not: {$eq: 24}}}).pretty()
Output

Explanation: This query will return documents where personal.age
is not equal to 24.
Example 3: Matching values in an array using $not
operator:
Let’s say we have a language
field in our documents that contains an array. In this example, we are retrieving only those employee’s documentswhere the language
is not Java or Perl.
Query:
db.contributor.find({language: {$not: {$in: ["Java", "Perl"]}}}).pretty()
Output

Explanation: This query will return documents where the language
is neither "Java" nor "Perl".
How $not
Interacts with Different Data Types
The $not
operator works across different data types, but it may behave differently depending on the data type. For example, using $not
with arrays or embedded documents can sometimes lead to unexpected results.
- Arrays: When used with arrays,
$not
negates the condition for each element in the array. - Null or Missing Fields: The
$not
operator will match documents where the field is missing, in addition to matching documents where the field's value does not satisfy the condition.
Best Practices for Using the $not
Operator
- Use with Other Operators: The
$not
operator cannot function independently. Always use it in combination with other operators like$gt
,$lt
,$eq
,$in
, etc., to define the condition you want to negate. - Optimize for Performance: While
$not
is powerful, it can sometimes impact query performance, especially when used with regular expressions or complex conditions. Always test your queries for efficiency when using$not
. - Avoid Complex Nested
$not
Queries: Nested$not
queries can become difficult to manage and may lead to unexpected behavior, especially with complex data types like arrays and embedded documents. Keep your queries simple and well-structured.
Conclusion
The MongoDB $not
operator is an essential tool for creating flexible and complex queries by negating specific conditions. Whether we're excluding certain values, filtering by range, or excluding documents based on regular expressions, $not
allows us to refine our queries and retrieve precisely the documents we need. By combining $not
with other operators and understanding its behavior with different data types, we can create efficient and powerful queries to meet our application's requirements.