Hey all,
I have been searching the internet forever trying to find an answer and nothing yet.
I want to return a document where some nested object field matches the search term. It would be a bonus if I could just return the nested object but the whole document is fine.
I have the following document for example, note how there are nested children within children, i want to traverse these children to look if the ‘slug’ property matches. I dont know how deep the nesting will go.
search term example: “backup-power/inverters/hybrid-inverters”
{
"_id": {
"$oid": "641841c3ae5e0d1f81c21df4"
},
"name": "backup-power",
"slug": "backup-power",
"label": "Back-up Power",
"children": [
{
"name": "batteries",
"slug": "backup-power/batteries",
"label": "Batteries",
"parent": "641841c3ae5e0d1f81c21df4"
},
{
"name": "inverters",
"slug": "backup-power/inverters",
"label": "Inverters",
"parent": "641841c3ae5e0d1f81c21df4",
"children": [
{
"name": "hybrid-inverters",
"slug": "backup-power/inverters/hybrid-inverters",
"label": "Hybrid Inverters",
"parent": "6418bf4cae5e0d1f81c21dfc"
}
]
},
{
"name": "portable-power",
"slug": "backup-power/portable-power",
"label": "Portable Power",
"parent": "641841c3ae5e0d1f81c21df4"
}
]
}
Any advice here? Should I just query the top level document and then on the server traverse the object? Should i build up a query on the server based on the slug that will tell mongodb exactly where to look? Or is there some query that can do this automatically?