例如树结构为;
[
{id: 1 , childrenIdList: [2, 3]},
{id: 2 , childrenIdList: [4, 5]},
{id: 3 , childrenIdList: []},
{id: 4 , childrenIdList: [6, 7]},
{id: 5 , childrenIdList: []},
{id: 6 , childrenIdList: []},
{id: 7 , childrenIdList: []}
]
就像;
1
2 3
4 5
6 7
如何跟踪从叶节点(id=7)到根(id=1)的树?
寻找父母id=7
很简单;
db.document.find({childrenList: { $in: [7]}}, {id: 1}).toArray(function(err), result{
/*result gives
{"id" : NumberInt(4)}
now I should look the parent of id=4, and parent of id=2 as you know.
*/
})
mongodb 可以进行递归查询吗?我该如何实施?