跨多个级别填充不起作用

问题描述 投票:0回答:1

我有博客模型有多个评论,有多个回复。我想通过所有这些,并且使用我的代码,我只能访问前2层。

这是我的DB作为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 }

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

node.js express mongoose
1个回答
1
投票

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

存在用于deeply populating nested moodels的mongoose插件,因此您可以使用以下代码轻松完成:

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并且您可以使用插件选项限制深度填充。

© www.soinside.com 2019 - 2024. All rights reserved.