您好,下面我已经指定了需要使用的数据并提供了输出结果,请问您可以帮我解决这个问题吗?我已经尝试了很多方法但是都没有找到解决方案。
//This is the data
let data = [
{'name': 'A', 'parent': null},
{'name': 'B', 'parent': null},
{'name': 'C', 'parent': 'A'},
{'name': 'D', 'parent': 'A'},
{'name': 'E', 'parent': 'D'},
{'name': 'F', 'parent': 'D'},
{'name': 'G', 'parent': 'B'},
{'name': 'H', 'parent': 'B'},
];
//And want the output like this by using mongodb aggregation
{
"null": [
{
"A": [
{
"C": []
},
{
"D": [
{
"E": []
},
{
"F": []
}
]
}
]
},
{
"B": [
{
"G": []
},
{
"H": []
}
]
}
]
}
我尝试使用图形查找聚合,但无法摆脱它。
谢谢您的帮助!