I want unspecified number of nested arrays output from graph lookup aggregation.
I have data collection:
[
{ _id: 1, name: 'AA' },
{ _id: 2, name: 'BB', parent: 1, ancestors: [1] },
{ _id: 3, name: 'CC' },
{ _id: 4, name: 'DD', parent: 3, ancestors: [3] },
{ _id: 5, name: 'DD', parent: 4, ancestors: [3, 4] },
{ _id: 6, name: 'FF', parent: 5, ancestors: [3, 4, 5] }
...,
...,
... ]
How to graph Lookup aggregate to output generates the unspecified hierarchy like this:
{
"_id" : 3,
"name" : "CC",
"children" : [
{
"_id" : 4,
"name" : "DD",
"parent" : 3,
"ancestors" : [
3
],
"children":[
{
"_id" : 5,
"name" : "DD",
"parent" : 4,
"ancestors" : [
3,
4
],
children:[
{
...,
children:[...]
}
]
},
]
},
]
}*
I hope anyone help me, thanks.