0

我正在尝试填充嵌套在其他引用中的引用。我让它工作了,但它似乎有点老套,想知道是否有其他方法可以做到这一点:

return Q.ninvoke(BoardSchema, 'find', {'_id': id}).then(function(board) {
    return Q.ninvoke(BoardSchema, 'populate', board, {path: 'lanes'}).then(function(board){
        return Q.ninvoke(LaneSchema, 'populate', board[0].lanes, {path: 'cards'}).then(function(lanes){
            board.lanes = lanes;
            return board;
        });
    });
});

是否有一些方法可以填充所有引用,或者返回第二个填充作为板调用的一部分,而不像现在这样手动设置它?

4

2 回答 2

1

您应该能够填充多个以填充嵌套文档,如下所示:

Item.find({}).populate('foo foo.child').exec(function(err, items) {
    // Do something here
});

这要求在 Schema 定义中设置 refs。

如果这不起作用,老实说大多数时候出于某种原因,你可以链接你的发现。但这与您的代码没有太大区别。

Item.find({}).populate('foo').exec(function(err, items) {
    Item.find(items).populate('bar').exec(function(err, items) {
        // Even more nests if you like
    });
});
于 2015-07-31T14:08:50.510 回答
0

基于 Gideon 的回应

Item.find({ _id: id})
  .populate({
    path: 'foo',
    model: 'FooModel',
    populate: {
      path: 'child',
      model: 'ChildModel'
    }
  })
  .exec(function(err, items) {
  // ...
});
于 2016-03-10T16:47:36.383 回答