My mongodb collection is stores records of groups which can have subgroups, which can further have sub-sub groups and there will not be any limits of nesting of this sub-groups. Is there anyway in mongoose to find a group name that resides in nth level?
My collection structure looks like the following, so suppose if i want to find if "My Group" has a subgroup named "My Group 1001", how am I supposed to find it?
{
"id": "60c18d4ce973fc139f23bf93",
"name": "My Group",
"subgroups": [
{
"id": "70c18d4ce973fc139f23bf93",
"name": "My Group 2",
"subgroups": [
{
"id": "80c18d4ce973fc139f23bf93",
"name": "My Group 3",
"subgroups": [
{
"id": "90c18d4ce973fc139f23bf93",
"name": "My Group 4",
"subgroups": [ ... ],
}
]
}
]
}
]
}
The other solutions that I found on stackoverflow suggested that it can be achieved using the dot notation for eg,
$or: [
{"name": "My Group 1001"},
{"subgroups.name": "My Group 1001"},
{"subgroups.subgroups.name": "My Group 1001"},
...
]
But since in my case the level is not known hence I cannot use the above pattern.