2015-09-19 61 views
-1

我定義的註釋模型:貓鼬如何居住在兩層以上?

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'}] 
}); 

當我使用的填充:

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

我發現貓鼬只有填充深一個層次。所以我該如何讓它填充多個等級,因爲等級可以是2或3或更多。

回答

0

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

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

更新:

它看起來像你的代碼工作不添加模型。所以問題應該在別的地方,而不是在人口稠密的地方。這就是你想要做的,對吧?

enter image description here

+0

謝謝。但我很遺憾,它不起作用。 – user3077147

+0

你確定'Comment.find({targetBlog:blog._id})'從數據庫中返回一個Comment嗎? – shan1024

+0

你確定添加'model:'Comment''會幫助貓鼬填充多個層次嗎? – user3077147