3.1 C
London
Friday, December 20, 2024
HomeSoftware TutorialsMongoDBMongoDB: How to Use a “NOT IN” Query

MongoDB: How to Use a “NOT IN” Query

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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"]}}) 

This query returns the following documents:

{ _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 here.

Additional Resources

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

MongoDB: How to Query with “Like” Regex
MongoDB: How to Check if Field Contains a String
MongoDB: How to Add a New Field in a Collection
MongoDB: How to Remove a Field from Every Document

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories