【发布时间】:2019-11-07 21:11:39
【问题描述】:
我需要您的帮助来进行聚合查询。我在发布之前进行了一些搜索,但没有找到对我有用的结果。
我有数据收集:
[
{ _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: 1,
name: 'AA',
child:
[
{
_id: 2,
name: 'BB',
parent: 1,
}
]
},
{
_id: 3,
name: 'CC',
child: [
{
_id: 4,
name: 'DD',
parent: 3,
child: [
{
_id: 5,
name: 'DD',
parent: 4
}
]
}
]
}
]
希望有人帮助我,谢谢。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework aggregate-functions aggregation