小编Mic*_*nik的帖子

mongoose递归填充

我一直在寻找,我没有找到任何好的答案.我有一个n-deep树,我存储在DB中,我想填充所有父母,所以最后我得到完整的树

node
 -parent
  -parent
    .
    .
    -parent
Run Code Online (Sandbox Code Playgroud)

到目前为止,我填充到2级,正如我所提到的,我需要达到n级.

Node.find().populate('parent').exec(function (err, items) {
   if (!err) {
     Node.populate(items, {path: 'parent.parent'}, function (err, data) {
       return res.send(data);
     });
   } else {
     res.statusCode = code;
     return res.send(err.message);
   }
 });
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

13
推荐指数
4
解决办法
9790
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1