Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to search in array of object in MongoDB?
To search in an array of objects in MongoDB, use the $elemMatch operator. This operator matches documents where at least one array element satisfies multiple query conditions within the same object.
Syntax
db.collection.find({
"arrayField": {
$elemMatch: {
"field1": "value1",
"field2": "value2"
}
}
});
Sample Data
Let us create a collection with employee documents ?
db.searchArrayDemo.insertMany([
{
"EmployeeFirstName": "Adam",
"EmployeeLastName": "Smith",
"EmployeeDateOfBirth": new ISODate("1992-01-31T13:45:10Z"),
"EmployeeSkills": ["Spring and Hibernate Framework", "Machine Learning"],
"EmployeeDetails": [
{
"EmployeePerformanceArea": "Java",
"Year": 2001
},
{
"EmployeePerformanceArea": "Python",
"Year": 2005
}
]
},
{
"EmployeeFirstName": "Carol",
"EmployeeLastName": "Taylor",
"EmployeeDateOfBirth": new ISODate("1993-04-21T11:10:20Z"),
"EmployeeSkills": ["C++", "Cloud Computing"],
"EmployeeDetails": [
{
"EmployeePerformanceArea": "C++",
"Year": 1998
},
{
"EmployeePerformanceArea": "C++ Game Developer",
"Year": 2007
}
]
}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5c77b3812386c62d05142a6a"),
ObjectId("5c77b58f2386c62d05142a6b")
]
}
Display all documents to verify the data ?
db.searchArrayDemo.find().pretty();
{
"_id": ObjectId("5c77b3812386c62d05142a6a"),
"EmployeeFirstName": "Adam",
"EmployeeLastName": "Smith",
"EmployeeDateOfBirth": ISODate("1992-01-31T13:45:10Z"),
"EmployeeSkills": [
"Spring and Hibernate Framework",
"Machine Learning"
],
"EmployeeDetails": [
{
"EmployeePerformanceArea": "Java",
"Year": 2001
},
{
"EmployeePerformanceArea": "Python",
"Year": 2005
}
]
}
{
"_id": ObjectId("5c77b58f2386c62d05142a6b"),
"EmployeeFirstName": "Carol",
"EmployeeLastName": "Taylor",
"EmployeeDateOfBirth": ISODate("1993-04-21T11:10:20Z"),
"EmployeeSkills": [
"C++",
"Cloud Computing"
],
"EmployeeDetails": [
{
"EmployeePerformanceArea": "C++",
"Year": 1998
},
{
"EmployeePerformanceArea": "C++ Game Developer",
"Year": 2007
}
]
}
Case 1: When Element is Found
Search for employees with C++ performance area in year 1998 ?
db.searchArrayDemo.find({
"EmployeeDetails": {
$elemMatch: {
"EmployeePerformanceArea": "C++",
"Year": 1998
}
}
}).pretty();
{
"_id": ObjectId("5c77b58f2386c62d05142a6b"),
"EmployeeFirstName": "Carol",
"EmployeeLastName": "Taylor",
"EmployeeDateOfBirth": ISODate("1993-04-21T11:10:20Z"),
"EmployeeSkills": [
"C++",
"Cloud Computing"
],
"EmployeeDetails": [
{
"EmployeePerformanceArea": "C++",
"Year": 1998
},
{
"EmployeePerformanceArea": "C++ Game Developer",
"Year": 2007
}
]
}
Case 2: When Element is Not Found
Search for non−existent combination of C performance area in year 1996 ?
db.searchArrayDemo.find({
"EmployeeDetails": {
$elemMatch: {
"EmployeePerformanceArea": "C",
"Year": 1996
}
}
}).pretty();
(No documents returned)
Key Points
-
$elemMatchensures all conditions are matched within a single array element. - Without
$elemMatch, conditions might match different array elements. - Returns the entire document if any array element matches all conditions.
Conclusion
Use $elemMatch to search array of objects when you need multiple conditions to match within the same array element. This ensures precise matching and prevents cross−element condition matching.
Advertisements
