MongoDB comes up with several different methods that can be used to find a document with an array that contains a specific value which we will explore in this article along with examples.
Assume the sample collection student
which is inserted with the documents having arrays and nested arrays. The document looks like this:
# Create collection
db.student.insertMany([
{
_id:1,
IT_Students:[
{ "name": "Kai" ,"age": 18, "email": "[email protected]" },
{ "name": "Emily", "age": 19, "email": "[email protected]" },
{ "name": "Candy", "age": 20, "email": "[email protected]" }
],
courses: [ "MongoDB", "Python", "PHP" ],
marks: [ 75, 80, 90 ]
},
{
_id:2,
Computer_Students:[
{ "name": "Milly" , "age": 21, "email": "[email protected]" },
{ "name": "Sam" , "age": 18, "email": "[email protected]" },
{ "name": "Andy" , "age": 22, "email": "[email protected]" }
],
courses: [ "Data Structure", "Calculus", "System Administration" ],
marks: [ 99, 70, 81 ]
},
{
_id:3,
BBA_Students:[
{ "name": "Twinkle" , "age": 24, "email": "[email protected]" },
{ "name": "Jenny" , "age": 19, "email": "[email protected]" },
{ "name": "Paul" , "age": 21, "email": "[email protected]" }
],
courses: [ "Accounts", "Finance", "Marketing" ],
marks: [ 73, 89, 91 ]
}
])
Now, explore the methods for finding a document with an array that contains a specific value.
1. Use the find() method
You can use the MongoDB find() method to find the document with an array that contains a specific value, the find()
method is the most common approach to get this requirement. Here’s an example of this. For the collection student
with documents that have an array field called courses
, we can use the query with the find()
method to find documents where courses
contains a specific value, let’s say “PHP”. The below search yields all the records that have PHP
in the courses
array section.
# Usage of find() method
# To find document by checking with an array
db.student.find({ courses: "PHP" })
As we can see in the output, the document with the array element PHP
is retrieved.
2. Find a Document from Nested Array
Similarly, we can also find the document from the nested array that contains a particular value by explicitly specifying the nested array element. For this, we used dot notation in the find()
method. In the following query, we use dot notation to specify that we are searching for documents where the BBA_Student
array contains the sub-array name
equals Paul
.
# Finding from Nested Document
db.student.find({"BBA_Students.name": "Paul"});
As a result, MongoDB returned the document that matched the criteria for the above query. The following images shows the output.
3. Using the $in method to Find Document in Array
Moreover, there is another technique that uses the $in
operator to identify documents where a field’s value matches any value in an array.
Here’s an example of how to use the $in
operator to get the document that includes a defined element. Firstly, the find()
method is specified with the array field marks
that has a $in
operator. Here, the $in
operator will find all the documents in the student
collection where the marks
array contains the value 90
.
# Usage of $in operator
db.student.find({ marks: { $in: [90] } })
In the output, a document whose _id
is equal to 1
is matched with the above $in
operator query criteria.
In addition, if we want to find documents where the array contains any of multiple values, we can simply pass an array of values to the $in
operator. In the following example, the $in
operator will return all documents where the courses
field contains MongoDB
or Finance
.
# Using $in operator to find multiple values
db.student.find({ courses: { $in: ["MongoDB", "Finance"] } })
As a result, we acquired the expected output below.
4. Using the $elemMatch to Find Document
Next, to find a document in MongoDB that contains an array with a specific value that meets certain conditions, you can use the $elemMatch
operator along with the find method.
Here is the query of $elemMatch
to find the document where the array Computer_Students
have an age greater than 18
.
# Usage of $elemMatch operator
db.student.find({
Computer_Students: {
$elemMatch: {
age: { $gt: 18 },
}
}
})
You will get the following output.
5. Using the $all method
Use $all
operator along with the find method to look for a value in an array. Follow the query of the $all
operator through which we want to find all documents that have both the values Accounts
and Marketing
in the array courses
.
# Usage of $all method
db.student.find({ courses: { $all: ["Accounts", "Marketing"] } })
Here is the resultant document.
6. Conclusion:
In this article, you have learned how to find a document with an array that contains a specific value, these techniques enable MongoDB users to quickly and precisely look for documents that comprise an array. However, the method chosen relies on the particular requirements of the query and the structure of the collection.
More details about this topic can be found here.