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 use watch in MongoDB?
- How to use unwind in MongoDB?
- How to rename a field in MongoDB?
- How to insert new document into MongoDB?
- How to remove a field from MongoDB?
- How to use triggers in MongoDB?
- How to do text search in MongoDB?
- How to use transactions in MongoDB?
- How to find by id in MongoDB?
- How to check the version of MongoDB?
See more codes...