Docs Menu
Docs Home
/ /

$ne (query predicate operator)

$ne

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

For comparison of different BSON type values, see the specified BSON comparison order.

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

The $ne operator has the following form:

{ field: { $ne: value } }

Note

If the value of $ne is null, see Non-Equality Filter.

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.

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:

db.movies.find(
{ rated: { $ne: "G" }, runtime: { $gt: 1000 } },
{ _id: 0, title: 1, runtime: 1, rated: 1 }
)
[
{
runtime: 1256,
title: 'Centennial'
},
{
runtime: 1140,
title: 'Baseball',
rated: 'TV-PG'
}
]

The following example sets the highestRated field based on a $ne comparison on a field in an embedded document. The updateMany() operation searches for an embedded document, imdb, with a subfield named rating. It uses $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:

db.movies.updateMany(
{ "imdb.rating": { $ne: 9.3 } },
{ $set: { "highestRated": false } }
)
{
acknowledged: true,
insertedId: null,
matchedCount: ...,
modifiedCount: ...,
upsertedCount: 0
}

The SQL equivalent to this query is:

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.

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.

The following examples return movies with a runtime over 1000 minutes to demonstrate each behavior.

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

db.movies.find(
{ genres: { $ne: "Drama" }, runtime: { $gt: 1000 } },
{ _id: 0, title: 1, genres: 1 }
)
[
{
genres: [ 'Documentary', 'History', 'Sport' ],
title: 'Baseball'
}
]

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" ]:

db.movies.find(
{ genres: { $ne: [ "Drama" ] }, runtime: { $gt: 1000 } },
{ _id: 0, title: 1, genres: 1 }
)
[
{
genres: [ 'Action', 'Adventure', 'Drama' ],
title: 'Centennial'
},
{
genres: [ 'Documentary', 'History', 'Sport' ],
title: 'Baseball'
}
]

Back

$lte

On this page