Visit complete MongoDB roadmap

← Back to Topics List

$nin

The $nin (Not In) operator is used to filter documents where the value of a field is not in a specified array. It selects the documents where the field value is either not in the specified array or the field does not exist.

Syntax

To use the $nin operator in a query, you can use the following syntax:

{ field: { $nin: [<value1>, <value2>, ..., <valueN>] } }

<field> is the name of the field you want to apply the $nin condition on, and <value1>, <value2>, ..., <valueN> are the values that the field should not have.

Example

Suppose you have a books collection with documents containing title and genre fields, and you want to find books that are not in the genres ‘Mystery’, ‘Sci-Fi’, or ‘Thriller’. You can use the $nin operator like this:

db.books.find({ genre: { $nin: ['Mystery', 'Sci-Fi', 'Thriller'] } });

This query will return all documents where the genre field is not one of the specified values or the field does not exist.

Conclusion

In summary, the $nin operator is a powerful tool that allows you to filter documents based on the absence of specific values in an array. By incorporating $nin into your MongoDB queries, you can effectively narrow down your search and retrieve the desired documents more efficiently.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.