$nin Operator in MongoDB

Learn via video courses
Topics Covered

Overview

The $nin query operator in MongoDB stands for "not in MongoDB". It is used to search for documents where a field's value does not match any of the values supplied in an array. In MongoDB, the $nin is primarily utilized as part of the query criteria in the find() or findOne() methods. It is compatible with all data types, including strings, numbers, arrays, and embedded documents. To exclude documents that match any of the criteria supplied in the array argument of $nin, multiple values can be specified. In MongoDB, the $nin query operator can be combined with other query operators to generate more complex queries and conditions.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of $nin in MongoDB

The field parameter specifies the name of the field to be compared.

The $nin i.e., not in MongoDB requires an array of values as input. It retrieves documents from the query results where the field value does not match any of the values in the array.

Examples of $nin Operator in MongoDB

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

MongoDB $nin Operator for Matching Values

Let's say we have a collection called "products" with documents representing different products. Each document has a "category" field. We want to find all products that are not in the categories "Electronics" or "Clothing".

This query will return all documents from the "products" collection where the "category" field does not have the values "Electronics" or "Clothing". It will exclude products in those categories and include products from other categories.

Using $nin in MongoDB to Update Data

Consider the "users" collection, which contains documents representing various users. Each document includes a "status" field that indicates the user's current state. Except for those with the status "admin" or "superadmin," we wish to change all users' status to "inactive."

This update query will set the "status" field to "inactive" for all documents in the "users" collection except those with the "status" field set to "admin" or "superadmin". It effectively changes the status of regular users to "inactive" while leaving the status of admin and superadmin unaltered.

FAQs

Q: Is the $nin operator case-sensitive?

A: The $nin (i.e., not in MongoDB) operator performs case-sensitive comparisons by default. However, you can use various options like $regex or $options to perform case-insensitive matches if needed.

Q: Can I use the $nin operator in both find() and update() operations?

A: Yes, the $nin operator can be used in both find() and update() operations. In find(), it helps filter the query results, while in update(), it helps determine which documents should be updated based on the specified values.

Conclusion

  • The $nin operator (i.e., not in MongoDB) is used to choose documents where the value of a field does not equal any of the array's specified values.
  • It compares the provided field's value to each value in the array and returns documents when the field value is not equal to any of the array's values.
  • The $nin operator is versatile and can be used with any MongoDB data type.
  • You can exclude documents based on multiple criteria by including them in the array argument.
  • To generate complicated and exact inquiries, combine the $nin operator with other query operators.
Hiring Partners:
GGoogleAAmazonMicrosoftFFlipkartAAdobe1200+ more