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 use watch in MongoDB?
- How to update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...