How to use “not equal” in MongoDB queries?

In MongoDB, the “not equal” operator is used to query documents where a field does not equal a specified value. This operator is denoted by the $ne operator and can be used in combination with logical operators such as $and and $or to form complex queries. To use the “not equal” operator, the syntax is {field: {$ne: value}}, where field is the name of the field to query, and value is the value to not equal.


You can use the $ne operator (which stands for “not equal”) in MongoDB to query for documents where a field is not equal to a certain value.

This operator uses the following basic syntax:

db.myCollection.find({'team': {$ne : "Mavs"}})

This particular example finds all documents in the collection titled myCollection where the team field is not equal to “Mavs.”

You can also use the $nin operator (which stands for “not in”) to query for documents where a field is not equal to any value in a list.

This operator uses the following basic syntax:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})

This particular example finds all documents in the collection titled myCollection where the team field is not equal to “Mavs”, “Cavs”, or “Spurs.”

The following examples show how to use each method in practice with a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})

Example 1: “Not Equal” Query

The following code shows how to find all documents in the teams collection where the “team” field is note equal to “Mavs”:

db.teams.find({'team': {$ne : "Mavs"}})

This query returns the following documents:

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 

Notice that every document in the teams collection is returned where the team field is not equal to “Mavs.”

Note: The $ne operator is case-sensitive.

Example 2: “Not In” Query

The following code shows how to find all documents in the teams collection where the team field is not equal to “Mavs”, “Cavs”, or “Spurs”:

db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})

This query returns the following documents:

{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 

Notice that every document in the teams collection is returned where the team field is not equal to “Mavs”, “Cavs”, or “Spurs.”

Note #1: You can find the complete documentation for the $ne function .

Note #2: You can find the complete documentation for the $nin function .

The following tutorials explain how to perform other common operations in MongoDB:

MongoDB: How to Query for “not null” in Specific Field

x