Skip to content

MongoDB find() using in ($in Operator) Naveen (NNK) Spark By {Examples}

  • by

In MongoDB, find() using in ($in operator) is used to match documents based on the values of a specified array field. The $in operator matches documents that contain any of the values in a specified array field. 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 any value in an array field. 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. Find documents with any matching values in an array using find in

In this section of the example, we used the $in operator within the find() method to find the documents that match any of the specified values in an array. With this operator, you can choose the documents whose field values match any of the specified values in the array. Additionally, if the field includes an array, this operator only chooses documents with fields that include arrays that have at least one object that matches a value of the provided array.

The query is given below for the $in operator.

#Usage of $in operator
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.

3. Find documents with any matching regular expression using find in

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.

4. Conclusion

In conclusion, the $in operator with find() method is used to retrieve documents from the MongoDB collection where the array contains any value. Here, you have learned the syntax and usage of $in operator with examples.

 In MongoDB, find() using in ($in operator) is used to match documents based on the values of a specified array field. The $in operator matches documents that contain any of the values in a specified array field. Use find() with all operator to match all values in an array. Here, we will discuss $in operator  Read More Uncategorized 

Leave a Reply

Your email address will not be published. Required fields are marked *