MongoDB find() using in ($in Operator)

In MongoDB, find() using in ($in operator) is used to match documents where the field contains any values in an array. The $in operator matches documents where the field matches to any value in the array. Use find() with all operator to match all values in an array.

Here, we will discuss $in operator and use it with the find() method to retrieve documents from MongoDB collection that matches a field contains any value in an array. So, first, let’s create a collection student and use this to explain $in operator with examples. 


# Create collection
db.student.insertMany([
      {  
         _id: 1,
         name: "Steve",
         info: [{"gender": "male", "age": 20}],
         hobbies: ["Swimming", "Reading"],
         course: ["MongoDB", "Python"],
         result: [{"marks":[70,80], "grades":["A","B"]}]
      },
      {
         _id: 2,
         name: "Alexa",
         info: [{"gender": "female", "age": 22}],
         hobbies: ["Playing", "Drawing"],
         course: ["Java", "Python"],
         result: [{"marks":[80,99], "grades":["A","C"]}]
      },
      {
         _id: 3,
         name: "Alice",
         info: [{"gender": "female", "age": 22}],
         hobbies: ["Gym", "Travelling"],
         course: ["C++", "Python"],
         result: [{"marks":[80,99], "grades":["B","C"]}]
      },
      {
         _id: 4,
         name: "Stefan",
         info: [{"gender": "male", "age": 23}],
         hobbies: ["Swimming", "Reading"],
         course: ["MongoDB", "Perl"],
         result: [{"marks":[85,95], "grades":["A","B"]}]
      }
   ]
)

1. MongoDB find() with $in Operator Syntax

Following is the syntax of the MongoDB find() with the $in operator.


# Syntax of using $in operator
db.collection.find(
  { <field>: { $in: [<value1>, <value2>, ... <valueN> ] } }
)

Here, the <field> should be of array type.

2. MongoDB find() with $in Usage with Example

Use the find() with the $in operator in MongoDB to find the documents where the field contains or matches any value from the specified array. It can also be used to retrieve any of the specified values contain within the specified array field. In other words, the $in operator retrieves the documents where the array field or a regular field contains any of the specified fields. Let’s take the following query.


# Using $in operator
{ name: { $in: [ "Steve", "Ram" ] } }

Here, name is a regular field. It returns all documents where the name is equal to Steve or Ram.

3. Using $in Operator with Array

When it is used with the array field, the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array. With this operator, you can choose the documents whose field values match any of the specified values in the array.

Here is an example of the $in operator using with find().


# Usage of $in operator with array field
db.student.find( { course: { $in: [ "MongoDB", "Python" ] } } )

The query here uses a find() method for searching the student collection. The find() method takes an array field course that we want to query. Then, the course field is further set with the $in operator. The $in operator finds documents whose fields contain any of the array’s given values. So, the query has returned all documents from the student collection in the output where the course field contains either MongoDB or Python values.

MongoDB find in

3. $in Operator with Regular Expression

Moreover, the $in operator of the MongoDB find() method helps us to find the documents that matched any of the regular expressions specified to it.

For example, we have applied a query below.


# Usage of regular expressions
db.student.find( { name: { $in: [ /^El/, /^St/ ] } } )

In this query of MongoDB, we used the find() method which is specified with the field name that we want to find. Then, it takes the $in operator that matches documents where the field matches any of the specified regular expressions. The regular expression ^El matches any string that starts with El, and the regular expression ^St matches any string that starts with St. Hence, we get the document that matched one of the regular expressions that are St because El is not found in any of the string names.

MongoDB find in

4. Conclusion

In conclusion, the $in operator with find() method is used to retrieve documents from the MongoDB collection where the field contains any value from the array where the field can be regular, array or nested field. You have also learned the syntax and usage of $in operator with examples.

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ @ LinkedIn

Leave a Reply

You are currently viewing MongoDB find() using in ($in Operator)