Definition
$not$notperforms a logicalNOToperation on the specified<operator-expression>and selects the documents that do not match the<operator-expression>. This includes documents that do not contain thefield.
Compatibility
You can use $not for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The $not operator has the following form:
{ field: { $not: { <operator-expression> } } }
Consider the following example:
db.movies.find( { runtime: { $not: { $gt: 180 } } } )
The example selects all documents in the movies collection where:
the
runtimefield value is less than or equal to180orthe
runtimefield does not exist
{ $not: { $gt: 180 } } differs from the $lte
operator. { $lte: 180 } returns only the documents where the
runtime field exists and its value is less than or equal to
180.
Use the $not operator with another operator expression. To use
$not for an inequality check, use:
{ runtime: { $not: { $eq: 120 } } }
The preceding query is equivalent to:
{ runtime: { $ne: 120 } }
The following query is invalid because it compares a field without an operator:
{ runtime: { $not: 120 } }
Behavior
Arrays
The $not operator can yield unexpected results when used with an
array. To match documents based on multiple false conditions, use
$nor.
Regular Expressions
The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
$not supports logical NOT operations on:
Regular expression objects, such as
/pattern/.The following example returns movies where
runtimeis greater than1000minutes andtitledoes not start with the letterT. Because$notalso matches documents that don't contain thetitlefield, the query returns movies even when title data is unavailable:db.movies.find( { title: { $not: /^T/ }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, runtime: 1 } ) [ { title: 'Centennial', runtime: 1256 }, { title: 'Baseball', runtime: 1140 } ] $regexoperator expressions.The following two queries return movies where
runtimeis greater than1000minutes andtitledoes not start with the letterT. The first query passes a string to$regex:db.movies.find( { title: { $not: { $regex: "^T" } }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, runtime: 1 } ) [ { title: 'Centennial', runtime: 1256 }, { title: 'Baseball', runtime: 1140 } ] The second query passes a regex literal to
$regex:db.movies.find( { title: { $not: { $regex: /^T/ } }, runtime: { $gt: 1000 } }, { _id: 0, title: 1, runtime: 1 } ) [ { title: 'Centennial', runtime: 1256 }, { title: 'Baseball', runtime: 1140 } ] Driver language regular expression objects.
For example, the following PyMongo query uses Python's
re.compile()method to compile a regular expression:import re for noMatch in db.inventory.find( { "item": { "$not": re.compile("^p.*") } } ): print noMatch