$in mongodb
Overview
One of the most commonly used features of MongoDB is the $in operator. The $in MongoDB operator allows you to match documents based on a list of values. In this article, we will explore the Mongodb $in operator in depth. We will cover how to use it when to use it, and the benefits it provides.
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 more$in In MongoDB
The MongoDB in the operator is used to match documents based on a list of values. The syntax for using the mongodb in operator is as follows:
The field represents the field in the document that you want to match against the list of values. The $in is the operator that performs the matching. The values are the list of values that you want to match against the field. The mongodb in the operator is useful when you want to match documents` based on a list of values.
Examples
To understand, how to use the $in the MongoDB operator, let's connect to our database using Mongo shell. To connect to the Mongo shell, open your command prompt and type mongoose, and then select the database you want to use. To select a database user use keyword followed by the db name.
We have a database named company with a collection named employees that stores various data of the employees of the company. Please run this command to populate the db. This will help you to keep up with the tutorial on $in MongoDB.
Here are some examples using the mongodb $in operator
Using $in to Match Values:
Suppose we want to find all the employees whose performance rating is either 4.2 or 3.8. We can use the $in MongoDB operator to match the values of the "performance_rating" field against an array of values that we're interested in: Our query will be:

How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Using $in to Match Values in an Array:
For example, we want to find all employees who work in either the "Marketing" or "Engineering" departments. We can do this by using the $in operator to match the department field against an array of possible values:

Using $in Operator to Match Values in Embedded/Nested Document:
Suppose we want to find all employees who are located in either "Mumbai" or "Kolkata". We can do this by using the $in operator to match the location. city field against an array of possible values:
This query will return two documents from the collection because Rahul Verma is located in "Mumbai" and Nandini Sen is located in "Kolkata":

Turn Learning into Career Growth
Updating data using $in operator
The $in operator can also be used to update data in MongoDB. For example, suppose we want to give a bonus to all employees who work in either the "Marketing" or "Sales" departments. We can do this by using the inc operator to increment the performance_rating field by 0.5:
This query will update the performance_rating field for both Rahul Verma and Akash Jha because they both work in either the "Marketing" or "Sales" departments. Nandini Sen will not be updated because she works in the "Engineering" department.

Using the $in Operator with a Regular Expression
The MongoDB in the MongoDB operator with a regular expression to accomplish this:
This query will return one document from the collection because Rahul Verma's full name contains the string "Ver":

FAQs
Q. What Are Some Use Cases For the $in MongoDB Operator?
A. The $in operator is useful when you want to match documents based on a list of values, particularly when you have a field in your documents that contains a finite set of values. For example, it can be used to find all documents where a field matches one of several possible values.
Q. Can you Use the $in Operator With an Empty Array in MongoDB?
A. Yes, you can use the $in operator with an empty array, but it will not match any documents in the collection.
Q. What is the Difference Between Using the or Operator in MongoDB?
A. The $in operator allows you to match documents based on a list of values, while the $or operator allows you to match documents based on multiple conditions. The $in operator is useful when you want to match documents against a finite set of values, while the $or operator is useful when you want to match documents based on more complex conditions.
Conclusion
- The $in MongoDB operator is a powerful tool that allows you to match documents based on a list of values.
- It simplifies queries that would otherwise require multiple conditions to be met, and it allows you to easily search for documents that match any value in a list.
- The mongodb is particularly useful when you have a field in your documents that contains a finite set of values.
: