跨多个级别填充不起作用

时间:2019-03-24 16:34:03

标签: node.js express mongoose

我有一个博客模型,该模型具有多个评论和多个回复。我想遍历所有这些内容,而使用我的代码,我只能访问前两层。

这在我的数据库中作为Comment对象:

{ "_id" : ObjectId("5c9770fc0147e40f1c53aab8"), "comments" : [ ObjectId("5c97734db48ab60f6139ffa3") ], "author" : ObjectId("5c9770e60147e40f1c53aab5"), "text" : "comment", "__v" : 1 }
{ "_id" : ObjectId("5c97734db48ab60f6139ffa3"), "comments" : [ ObjectId("5c977a169f98a8106dc0dc5c") ], "text" : "2", "__v" : 1 }
{ "_id" : ObjectId("5c977a169f98a8106dc0dc5c"), "comments" : [ ], "text" : "3", "__v" : 0 }
{ "_id" : ObjectId("5c977b3b3a5bb410ce7e0e70"), "comments" : [ ObjectId("5c977b533a5bb410ce7e0e71") ], "author" : ObjectId("5c9770e60147e40f1c53aab5"), "text" : "1", "__v" : 1 }
{ "_id" : ObjectId("5c977b533a5bb410ce7e0e71"), "comments" : [ ObjectId("5c977b6f3a5bb410ce7e0e73") ], "author" : ObjectId("5c9770e60147e40f1c53aab5"), "text" : "2", "__v" : 1 }
{ "_id" : ObjectId("5c977b6f3a5bb410ce7e0e73"), "comments" : [ ], "author" : ObjectId("5c9770e60147e40f1c53aab5"), "text" : "3", "__v" : 0 }

评论模式:

var commentSchema = new mongoose.Schema({
    author: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "User"
    },
    text: String,
    comments: [{
        type: mongoose.Schema.Types.ObjectId, 
        ref: "Comment"
    }]
});

博客对象:

{ "_id" : ObjectId("5c9770ee0147e40f1c53aab6"), "comments" : [ ObjectId("5c9770fc0147e40f1c53aab8"), ObjectId("5c977b3b3a5bb410ce7e0e70") ], "title" : "title", "image" : "", "text" : "text", "author" : ObjectId("5c9770e60147e40f1c53aab5"), "date" : ISODate("2019-03-24T11:58:38.227Z"), "__v" : 2 }

博客架构:

var blogSchema = new mongoose.Schema({
    title: String,
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    },
    date: ({type: Date, default: Date.now}),
    text: String,
    image: String,
    comments: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Comment"
    }]
})

这是我的路线

router.get("/:id",function(req,res){
    Blog.findById(req.params.id)
    .populate("author")
    .populate({
        path: "comments",
        model: "Comment",
        populate: {
            path: "comments",
            model: "Comment"
        }
    })
    .exec(function(err,blog){
        if (err) console.log(err);

        res.render("../views/blogs/show",{blog: blog});
    })
});

这是我打印博客时得到的:

{ comments: [ { comments: [Array], _id: 5c97734db48ab60f6139ffa3, text: '2', __v: 1 } ], _id: 5c9770fc0147e40f1c53aab8, author: 5c9770e60147e40f1c53aab5, text: 'comment', __v: 1 },{ comments: [ { comments: [Array], _id: 5c977b533a5bb410ce7e0e71, author: 5c9770e60147e40f1c53aab5, text: '2', __v: 1 } ], _id: 5c977b3b3a5bb410ce7e0e70, author: 5c9770e60147e40f1c53aab5, text: '1', __v: 1 }

如何填充所有嵌套的注释对象数组?

1 个答案:

答案 0 :(得分:1)

isn't a direct support个用于检索潜在的无限嵌套对象引用(Comment-> Comment-> Comment-> ...)。由于性能原因,这是显而易见的。

存在deeply populating nested moodels的猫鼬插件,因此您可以使用以下代码轻松实现:

const deepPopulate = require('mongoose-deep-populate')(mongoose);
blogSchema.plugin(deepPopulate, {}); //<- {} empty options

// ...

Blog.findById(req.params.id)
.populate('author')
.deepPopulate('comments.user')
.exec(function (err, blog) {
    if (err) console.log(err);
    res.render("../views/blogs/show",{blog: blog});
});

请阅读readme,以获取有关该插件的许多有用信息,并记住can be really inefficient的深度填充,并且您可以使用插件的选项限制深度填充。 / p>