How to use a “NOT IN” Query in MongoDB?

The MongoDB “NOT IN” query is used to find documents in a collection that do not match a set of values. It is a logical operator that is used to compare two expressions and returns true if the first expression does not equal the second expression. The “NOT IN” query can be used in a MongoDB find() query to find documents that do not match the set of values specified in the “NOT IN” clause. This will return all documents that do not match the values in the “NOT IN” clause.


You can use the following syntax to query for all documents where the value for a particular field is not in a certain list of values:

db.collection.find({field1: {$nin: ["value1", "value2", "value3"]}}) 

This particular query finds all documents where the value in field1 is not equal to value1, value2, or value3.

The following examples show how to use this syntax in practice.

Example 1: Query for “NOT IN” with One Value

Suppose we have a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", position: "Guard", points: 31})
db.teams.insertOne({team: "Spurs", position: "Guard", points: 22})
db.teams.insertOne({team: "Rockets", position: "Center", points: 19})
db.teams.insertOne({team: "Warriors", position: "Forward", points: 26})
db.teams.insertOne({team: "Cavs", position: "Guard", points: 33})

We can use the following code to find all documents where the “team” field is not equal to the value “Rockets”:

db.teams.find({team: {$nin: ["Rockets"]}}) 

This query returns the following documents:

{ _id: ObjectId("619527e467d6742f66749b72"),
  team: 'Cavs',
  position: 'Guard',
  points: 33 }

{ _id: ObjectId("619527e467d6742f66749b6e"),
  team: 'Mavs',
  position: 'Guard',
  points: 31 }

{ _id: ObjectId("619527e467d6742f66749b6f"),
  team: 'Mavs',
  position: 'Guard',
  points: 22 }

Notice that the only documents returned are the ones where the “team” field is not equal to “Rockets.”

Example 2: Query for “NOT IN” with List of Values

Suppose we have a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", position: "Guard", points: 31})
db.teams.insertOne({team: "Spurs", position: "Guard", points: 22})
db.teams.insertOne({team: "Rockets", position: "Center", points: 19})
db.teams.insertOne({team: "Warriors", position: "Forward", points: 26})
db.teams.insertOne({team: "Cavs", position: "Guard", points: 33})

We can use the following code to find all documents where the “team” field is not equal to the value “Rockets” or “Cavs”:

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

{ _id: ObjectId("619527e467d6742f66749b6e"),
  team: 'Mavs',
  position: 'Guard',
  points: 31 }

{ _id: ObjectId("619527e467d6742f66749b6f"),
  team: 'Mavs',
  position: 'Guard',
  points: 22 }

Notice that the only documents returned are the ones where the “team” field is not equal to “Rockets” or “Cavs.”

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

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

x