簡體   English   中英

貓鼬如何填充超過 2 級的深度?

[英]How mongoose populate more than 2 level deep?

我將評論模型定義為:

CommentSchema = new mongoose.Schema ({
    name: String,
    email: String,
    website: String,
    content: String,
    createDate: Date,
    updateDate: Date,
    targetBlog: {type: mongoose.Schema.Types.ObjectId, ref: 'Blog'},
    childrenComment: [{type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}]
});

當我使用 populate 時:

Comment.find({targetBlog: blog._id}).populate({path: 'childrenComment'}).exec(function(err, comments) {
    console.log(comments);
    res.render('blog', { blog: blog, comments: comments});
});

我發現貓鼬只填充一層深。 那么我該怎么做才能讓它填充多個級別,因為級別可以是 2 或 3 級或更多。

您可以在填充時手動指定模型。

Comment.find({targetBlog: blog._id})
    .populate({path: 'childrenComment', model: 'Comment'})
        .exec(function(err, comments) {
            console.log(comments);
            res.render('blog', { blog: blog, comments: comments});
});

更新:

看起來您的代碼無需添加模型即可運行。 所以問題應該出在其他地方,而不是人口中。 這就是你想要做的,對吧?

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM