HOWTO · MongoDB

The $ne Operator in MongoDB

Discover the $ne operator in MongoDB, also known as the not equal operator. Learn how to use it effectively in find and update methods with practical examples and clear explanations. Enhance your database querying and manipulation skills with this powerful tool.

On this page

MongoDB’s $ne query operator selects documents whose field value is not equal to a specified value. It is useful when a query should exclude a value, and it can appear in both read filters and update filters. One detail matters when interpreting its results: a document also matches $ne when the field is absent. If your task requires the field to exist, add an $exists: true condition alongside the inequality.

What the $ne Operator Matches

The expression { field: { $ne: value } } tests whether field differs from value. For example, { city: { $ne: "New York" } } selects documents whose city is not "New York", as well as documents with no city field. It does not mean “the field exists and has another value.” That distinction can affect counts and downstream updates when records have incomplete data.

The comparison is against the value you supply. For an array-valued field, MongoDB’s query matching rules can make the result less obvious than a simple comparison of two scalar values; check the data shape and the operator documentation before relying on array behavior. This guide focuses on ordinary scalar fields.

Use $ne in a Query

In the MongoDB shell, pass the condition to find. For instance, db.users.find({ city: { $ne: "New York" } }) requests users whose city is different from "New York" or is missing. String values need quotation marks, and the operator name begins with $. The query is a filter: it does not alter matching documents.

If missing cities should not qualify, combine the conditions: db.users.find({ city: { $ne: "New York", $exists: true } }). This explicitly requires city to be present while excluding the specified value. Adapt users, city, and "New York" to the collection, field, and value in your data.

These are MongoDB shell expressions, not standalone programs; they require a MongoDB server and collection to produce database results. No execution evidence was supplied for this article, so no result set or output is claimed here.

Use $ne in an Update

An update filter uses $ne in the same way as a read filter. For example, db.users.updateMany({ city: { $ne: "New York" } }, { $set: { status: "review" } }) asks MongoDB to set status to "review" on documents whose city is not "New York" or is missing. The filter determines which documents are eligible; $set describes the change.

Use the filter carefully: because absent city fields match, this update also includes documents without a city. To exclude them, add city: { $ne: "New York", $exists: true } to the filter. updateMany applies the update to all matching documents, but the number modified can be smaller than the number matched—for example, a document already holding the requested status may require no change. Review the filter against the intended records before running a bulk update. The expression above is syntax guidance and has not been run against a database in this packet.

$ne and $not: How They Differ

$ne expresses inequality with a value: { status: { $ne: "closed" } } asks for a status that differs from "closed", and also matches documents where status is absent. $not negates an operator expression, as in { price: { $not: { $gt: 100 } } }. In that pattern, the inner $gt describes a comparison and $not negates that expression.

Choose $ne when the condition is specifically “not equal to this value.” Use $not when the condition is the negation of another supported operator expression. Missing-field behavior can matter to either query, so consult MongoDB’s operator documentation when the field may be absent or the expression is more complex: $ne examples and $not.