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
Search for a text in MongoDBs Double Nested Array?
To search for a text in MongoDB's double nested array, use dot notation to traverse through multiple array levels. The dot notation format is parentArray.childArray.fieldName to target specific fields deep within nested structures.
Syntax
db.collection.find({
"parentArray.childArray.fieldName": "searchValue"
});
Sample Data
Let us create a collection with double nested array documents ?
db.doubleNestedArrayDemo.insertMany([
{
"StudentId": "1000",
"StudentName": "Larry",
"StudentDetails": [
{
"ProjectName": "Online Banking",
"ProjectDetails": [
{
"TechnologyUsed": "Java"
},
{
"TechnologyUsed": "MySQL in Backend"
}
]
}
]
},
{
"StudentId": "1001",
"StudentName": "Robert",
"StudentDetails": [
{
"ProjectName": "Student Web Tracker",
"ProjectDetails": [
{
"TechnologyUsed": "Django Framework"
},
{
"TechnologyUsed": "MongoDB in Backend"
}
]
}
]
}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5c992bd7330fd0aa0d2fe4cc"),
ObjectId("5c992cdb330fd0aa0d2fe4cd")
]
}
Example: Search for "Java" Technology
To find records containing "Java" in the double nested array ?
db.doubleNestedArrayDemo.find({
"StudentDetails.ProjectDetails.TechnologyUsed": "Java"
});
{
"_id": ObjectId("5c992bd7330fd0aa0d2fe4cc"),
"StudentId": "1000",
"StudentName": "Larry",
"StudentDetails": [
{
"ProjectName": "Online Banking",
"ProjectDetails": [
{
"TechnologyUsed": "Java"
},
{
"TechnologyUsed": "MySQL in Backend"
}
]
}
]
}
How It Works
-
StudentDetailsis the first array level -
ProjectDetailsis the second (nested) array level -
TechnologyUsedis the target field within the double nested structure - MongoDB automatically searches through all array elements at each level
Conclusion
Use dot notation with the pattern parentArray.childArray.fieldName to search within double nested arrays. MongoDB traverses all array elements automatically to find matching documents.
Advertisements
