> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $not (query predicate operator)

## Definition

[`$not`](https://www.mongodb.com/docs/manual/reference/operator/query/not.md#mongodb-query-op.-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`.

## Compatibility

You can use `$not` for deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The [`$not`](https://www.mongodb.com/docs/manual/reference/operator/query/not.md#mongodb-query-op.-not) operator has the following form:

```javascript
{ field: { $not: { <operator-expression> } } }
```

Consider the following example:

```javascript
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`](https://www.mongodb.com/docs/manual/reference/operator/query/lte.md#mongodb-query-op.-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:

```javascript
{ runtime: { $not: { $eq: 120 } } }
```

The preceding query is equivalent to:

```javascript
{ runtime: { $ne: 120 } }
```

The following query is invalid because it compares a field without an operator:

```javascript
{ 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`.](https://www.mongodb.com/docs/manual/reference/operator/query/nor.md#mongodb-query-op.-nor)

### Regular Expressions

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). 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`](https://www.mongodb.com/docs/manual/reference/operator/query/not.md#mongodb-query-op.-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:

  ```javascript
  db.movies.find(
     { title: { $not: /^T/ }, runtime: { $gt: 1000 } },
     { _id: 0, title: 1, runtime: 1 }
  )

  ```

  **Output:**

  ```javascript
  [
    { title: 'Centennial', runtime: 1256 },
    { title: 'Baseball', runtime: 1140 }
  ]

  ```

- [`$regex`](https://www.mongodb.com/docs/manual/reference/operator/query/regex.md#mongodb-query-op.-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`:

  ```javascript
  db.movies.find(
     { title: { $not: { $regex: "^T" } }, runtime: { $gt: 1000 } },
     { _id: 0, title: 1, runtime: 1 }
  )

  ```

  **Output:**

  ```javascript
  [
    { title: 'Centennial', runtime: 1256 },
    { title: 'Baseball', runtime: 1140 }
  ]

  ```

  The second query passes a regex literal to `$regex`:

  ```javascript
  db.movies.find(
     { title: { $not: { $regex: /^T/ } }, runtime: { $gt: 1000 } },
     { _id: 0, title: 1, runtime: 1 }
  )

  ```

  **Output:**

  ```javascript
  [
    { title: 'Centennial', runtime: 1256 },
    { title: 'Baseball', runtime: 1140 }
  ]

  ```

- Driver language regular expression objects.

  For example, the following [PyMongo](https://pymongo.readthedocs.io/en/stable/index.html) query uses Python's `re.compile()` method to compile a regular expression:

  ```python
  import re
  for noMatch in db.inventory.find( { "item": { "$not": re.compile("^p.*") } } ):
      print noMatch
  ```

## Learn More

- [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find)

- [`$set`](https://www.mongodb.com/docs/manual/reference/operator/update/set.md#mongodb-update-up.-set)

- [`$gt`](https://www.mongodb.com/docs/manual/reference/operator/query/gt.md#mongodb-query-op.-gt)

- [`$regex`](https://www.mongodb.com/docs/manual/reference/operator/query/regex.md#mongodb-query-op.-regex)

- [`$eq`](https://www.mongodb.com/docs/manual/reference/operator/query/eq.md#mongodb-query-op.-eq)
