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

# $nin (query predicate operator)

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

- the specified field value is not in the specified array **or**

- the specified field does not exist.

## Compatibility

You can use `$nin` 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 [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) operator has the following form:

```javascript
{ field: { $nin: [ <value1>, <value2> ... <valueN> ] } }
```

## Behavior

### Type Bracketing

Unlike the inequality operators ([`$gt`](https://www.mongodb.com/docs/manual/reference/operator/query/gt.md#mongodb-query-op.-gt), [`$gte`](https://www.mongodb.com/docs/manual/reference/operator/query/gte.md#mongodb-query-op.-gte), [`$lt`](https://www.mongodb.com/docs/manual/reference/operator/query/lt.md#mongodb-query-op.-lt), and [`$lte`](https://www.mongodb.com/docs/manual/reference/operator/query/lte.md#mongodb-query-op.-lte)), `$nin` doesn't use type bracketing. Instead, it matches documents where the field value is not one of the specified values, regardless of the value's type.

For example, `{ a: { $nin: [ 2, 4 ] } }` also matches documents where `a` is a string.

### Match an Array Value

If `field` has an array, the [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) operator selects the documents whose `field` has an array with **no** element equal to a value in the specified array. For example, `<value1>`, `<value2>`, and so on.

## Examples

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.

### Select on Unmatching Documents

The following example returns movies where `runtime` is greater than `1000` minutes and `rated` is not in `[ "G", "PG" ]`. Because `$nin` also matches documents that don't contain the `rated` field, the query returns movies even when the rating data is unavailable:

```javascript
db.movies.find(
   { rated: { $nin: [ "G", "PG" ] }, runtime: { $gt: 1000 } },
   { _id: 0, title: 1, year: 1, rated: 1 }
)

```

**Output:**

```javascript
[
  { title: 'Centennial', year: 1978 },
  {
    title: 'Baseball',
    year: 1994,
    rated: 'TV-PG'
  }
]

```

### Select on Elements Not in an Array

The following example sets the `exclude` field to `true` for movies that don't have `"Drama"` in their `genres` array:

```javascript
db.movies.updateMany(
   { genres: { $nin: [ "Drama" ] } },
   { $set: { exclude: true } }
)

```

**Output:**

```javascript
{
  acknowledged: true,
  insertedId: null,
  matchedCount: ...,
  modifiedCount: ...,
  upsertedCount: 0
}

```

[`updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updatemany.md#mongodb-method-db.collection.updateMany) also selects a document when the document does not contain the field [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) is matching on.

The inequality operator [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) is *not* very selective since it often matches a large portion of the index. As a result, in many cases, a [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) query with an index may perform no better than a [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) query that must scan all documents in a collection. See also [Create Selective Queries.](https://www.mongodb.com/docs/manual/core/query-optimization.md#std-label-read-operations-query-selectivity)

## Learn More

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

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

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