Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

$not (query predicate operator)

$not

$not performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression>. This includes documents that do not contain the field.

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

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 runtime field value is less than or equal to 180 or

  • the runtime field 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 } }

The $not operator can yield unexpected results when used with an array. To match documents based on multiple false conditions, use $nor.

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 runtime is greater than 1000 minutes and title does not start with the letter T. Because $not also matches documents that don't contain the title field, 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 }
    ]
  • $regex operator expressions.

    The following two queries return movies where runtime is greater than 1000 minutes and title does not start with the letter T. 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

Back

$nor

On this page