Definition
$ne$neselects 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.
Compatibility
You can use $ne for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The $ne operator has the following form:
{ field: { $ne: value } }
Note
If the value of $ne is null, see Non-Equality Filter.
Examples
The following examples use the inventory collection. To create the
collection, run the following
insertMany() command in
mongosh:
db.inventory.insertMany( [ { item: "nuts", quantity: 30, carrier: { name: "Shipit", fee: 3 } }, { item: "bolts", quantity: 50, carrier: { name: "Shipit", fee: 4 } }, { item: "washers", quantity: 10, carrier: { name: "Shipit", fee: 1 } } ] )
Match Document Fields That Are Not Equal
The following example selects documents in the inventory collection
where quantity is not equal to 20, and includes documents that
don't contain the quantity field:
db.inventory.find( { quantity: { $ne: 20 } } )
{ _id: ObjectId("61ba667dfe687fce2f042420"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 } }, { _id: ObjectId("61ba667dfe687fce2f042421"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 } }, { _id: ObjectId("61ba667dfe687fce2f042422"), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } }
The SQL equivalent to this query is:
SELECT * FROM INVENTORY WHERE QUANTITIY != 20
Update Based on Not Equal Embedded Document Fields
The following example sets the price field based on a $ne
comparison on a field in an embedded document. The
updateMany() operation searches for an embedded
document, carrier, with a subfield named fee. It uses
$set to update the price field to 9.99 in each
document where the value of fee is not equal to 1 or where the
fee subfield does not exist:
db.inventory.updateMany( { "carrier.fee" : { $ne: 1 } }, { $set: { "price": 9.99 } } )
{ _id: ObjectId("61ba66e2fe687fce2f042423"), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, price: 9.99 }, { _id: ObjectId("61ba66e2fe687fce2f042424"), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 }, price: 9.99 }, { _id: ObjectId("61ba66e2fe687fce2f042425"), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } }
The SQL equivalent to this query is:
UPDATE INVENTORY SET PRICE = '9.99' WHERE carrierfee != 1
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.
Arrays
When comparing arrays, $ne matches documents where the document
array differs from the specified array in $ne. Specifically, $ne
matches any documents where the arrays:
Have different element values or strings.
Have elements in a different order.
Have a different number of elements.
Are missing from a document.
For example, the following query returns inventory documents where
the type array differs from [ "hardware", "fasteners" ]:
db.inventory.find( { type: { $ne: [ "hardware", "fasteners" ] } } )
The following complete example adds a type array to two
inventory documents and runs a query with $ne:
// Update the nuts document to include a type array db.inventory.updateOne( { item: "nuts" }, { $set: { type: [ "hardware" ] } } ) // Update the bolts document to include a type array db.inventory.updateOne( { item: "bolts" }, { $set: { type: [ "hardware", "fasteners" ] } } ) // Find documents where the type array differs from [ "hardware", "fasteners" ] db.inventory.find( { type: { $ne: [ "hardware", "fasteners" ] } } )
Output shows the nuts document because the array differs from [
"hardware", "fasteners" ], and the washers document because it
doesn't have a type array:
[ { _id: ObjectId('679d26907c5a58595234305c'), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, type: [ 'hardware' ] }, { _id: ObjectId('679d26907c5a58595234305e'), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } } ]
This query reverses the elements in the array:
db.inventory.find( { type: { $ne: [ "fasteners", "hardware" ] } } )
Because the type array in the bolts document is [ "hardware",
"fasteners" ], which differs from [ "fasteners", "hardware" ], the
query returns the bolts document in addition to the nuts and
washers documents:
[ { _id: ObjectId('679d26907c5a58595234305c'), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, type: [ 'hardware' ] }, { _id: ObjectId('679d26907c5a58595234305d'), item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 }, type: [ 'hardware', 'fasteners' ] }, { _id: ObjectId('679d26907c5a58595234305e'), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } } ]
$ne matches documents where the array doesn't contain the specified
value and documents that don't have the array. For example:
db.inventory.find( { type: { $ne: "fasteners" } } )
The query returns the nuts document because the array
[ "hardware" ] differs from "fasteners". The query also
returns the washers document because the document doesn't contain
a type array.
Query output:
[ { _id: ObjectId('679d26907c5a58595234305c'), item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, type: [ 'hardware' ] }, { _id: ObjectId('679d26907c5a58595234305e'), item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } } ]