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

# $ne (query predicate operator)

## Definition

`$ne` selects documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.

## Compatibility

You can use `$ne` 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 `$ne` operator has the following form:

```javascript
{ field: { $ne: value } }
```

**Note:**

If the value of `$ne` is null, see [Non-Equality Filter.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-non-equality-filter)

## 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)), `$ne` doesn't use type bracketing. Instead, it matches documents where the field value is not equal to the specified value, regardless of the value's type.

For example, `{ a: { $ne: 2 } }` also matches documents where `a` is a string.

### Array Comparison

When comparing arrays, `$ne` behaves differently depending on whether you pass a scalar or an array as the comparison value.

- **Scalar comparison**: `$ne` matches documents where the scalar value is not present as an element in the array, including documents that don't have the field.

- **Exact array comparison**: `$ne` matches documents where the field array is not identical to the specified array, including documents with a different element order, a different number of elements, or a missing field.

## 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.

### Match Document Fields That Are Not Equal

The following example returns movies where `runtime` is greater than `1000` minutes and `rated` is not equal to `"G"`. Because `$ne` 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: { $ne: "G" }, runtime: { $gt: 1000 } },
   { _id: 0, title: 1, runtime: 1, rated: 1 }
)

```

**Output:**

```javascript
[
  {
    runtime: 1256,
    title: 'Centennial'
  },
  {
    runtime: 1140,
    title: 'Baseball',
    rated: 'TV-PG'
  }
]

```

### Update Based on Not Equal Embedded Document Fields

The following example sets the `highestRated` field based on a `$ne` comparison on a field in an embedded document. The [`updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updatemany.md#mongodb-method-db.collection.updateMany) operation searches for an embedded document, `imdb`, with a subfield named `rating`. It uses [`$set`](https://www.mongodb.com/docs/manual/reference/operator/update/set.md#mongodb-update-up.-set) to update the `highestRated` field to `false` in each document where the value of `rating` is not equal to `9.3` or where the `rating` subfield does not exist:

```javascript
db.movies.updateMany(
   { "imdb.rating": { $ne: 9.3 } },
   { $set: { "highestRated": false } }
)

```

**Output:**

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

```

The SQL equivalent to this query is:

```sql
UPDATE movies SET highestRated = false WHERE imdb_rating != 9.3
```

The inequality operator `$ne` is *not* very selective since it often matches a large portion of the index. As a result, in many cases, a `$ne` query with an index may perform no better than a `$ne` 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)

### Arrays

The following examples show how `$ne` behaves differently depending on whether you pass a scalar or an array as the comparison value.

#### Use a Scalar Comparison

The following example returns movies where `runtime` is greater than `1000` minutes and `"Drama"` is not an element of the `genres` array:

```javascript
db.movies.find(
   { genres: { $ne: "Drama" }, runtime: { $gt: 1000 } },
   { _id: 0, title: 1, genres: 1 }
)

```

**Output:**

```javascript
[
  {
    genres: [ 'Documentary', 'History', 'Sport' ],
    title: 'Baseball'
  }
]

```

#### Use an Exact Array Comparison

The following example returns movies where `runtime` is greater than `1000` minutes and the `genres` array is not exactly equal to `[ "Drama" ]`. Unlike the scalar comparison, `Centennial` (with `genres: [ "Action", "Adventure", "Drama" ]`) matches because that array is not identical to `[ "Drama" ]`:

```javascript
db.movies.find(
   { genres: { $ne: [ "Drama" ] }, runtime: { $gt: 1000 } },
   { _id: 0, title: 1, genres: 1 }
)

```

**Output:**

```javascript
[
  {
    genres: [ 'Action', 'Adventure', 'Drama' ],
    title: 'Centennial'
  },
  {
    genres: [ 'Documentary', 'History', 'Sport' ],
    title: 'Baseball'
  }
]

```

## Learn More

- [SQL to MongoDB Mapping Chart](https://www.mongodb.com/docs/manual/reference/sql-comparison.md#std-label-sql-to-mongodb-mapping)

- [Read Documents](https://www.mongodb.com/docs/manual/tutorial/query-documents.md#std-label-read-operations-query-document)
