【发布时间】:2018-08-28 04:23:43
【问题描述】:
我是 mongodb 的新手,正在尝试聚合。我需要以下问题的帮助。 我有这样的收藏
{_id: 1, parentId: null, name: 'foo'},
{_id: 2, parentId: '1', name: 'boo'},
{_id: 3, parentId: '2', name: 'koo'},
{_id: 4, parentId: '3', name: 'coo'}
{_id: 5, parentId: '4', name: 'loo'}
我想执行聚合并获取 id 的父母和孩子的列表。我怎样才能做到这一点?提前致谢。
我尝试了 mongodb 的 graphLookup,但没有得到预期的结果。我试过这个。
db.files.aggregate([ { $graphLookup: {
from : 'files',
startWith: '$id',
connectFromField: 'parentId',
connectToField: 'id',
as: 'parents'
}}])
我需要的输出格式是:
{
id: id,
parents: [{id, name}],
children: []
}
【问题讨论】:
-
您希望收到的 JSON 输出究竟是什么?
标签: mongodb tree aggregation-framework