mongodbHow to query with "not in" condition in MongoDB?
MongoDB provides the $nin
operator to query documents with a "not in" condition. The $nin
operator is used to specify a query condition to select documents that do not match the specified values.
Example
db.collection.find({
field: {
$nin: [value1, value2, ...]
}
})
This example will return all documents in the collection where the value of the field is not equal to any of the specified values.
Parts of the code:
db.collection.find()
: This is the MongoDB query method used to find documents in a collection.field
: This is the name of the field in the document that is being queried.$nin
: This is the MongoDB operator used to specify a "not in" condition.[value1, value2, ...]
: This is an array of values that the field must not match.
Helpful links
More of Mongodb
- How to check the version of MongoDB?
- How to insert new document into MongoDB?
- How to use triggers in MongoDB?
- How to remove a field from MongoDB?
- How to rename a MongoDB collection?
- How to list databases in MongoDB?
- How to perform a health check for MongoDB?
- How to drop a database in MongoDB?
- How to use watch in MongoDB?
- How to update an array element in MongoDB?
See more codes...