$in mongodb

Learn via video courses
Topics Covered

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

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
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

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

$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

Sharpen Your Fundamentals with Free Learning

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:

in MongoDB

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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:

in mongoodb operator

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":

in MongoDB operator1

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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 inoperatortomatchthedepartmentfieldagainstanarrayofpossiblevalues,andthenusingthein operator to match the department field against an array of possible values, and then 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.

in mongoodb operator

Using the $in Operator with a Regular Expression

The MongoDB inoperatorcanalsobeusedincombinationwitharegularexpressiontomatchvaluesthatmatchaparticularpattern.Forexample,supposewewanttofindallemployeeswhosefullnamecontainsthestring"Ver".Wecanusethein operator can also be used in combination with a `regular expression` to match values that match a particular pattern. For example, suppose we want to find all employees whose full name contains the string "Ver". We can use the 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":

operator with regular expression

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 inOperatorandthein Operator and 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

  1. The $in MongoDB operator is a powerful tool that allows you to match documents based on a list of values.
  2. 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.
  3. The mongodb is particularly useful when you have a field in your documents that contains a finite set of values.

: