我有 Category
模型:
Category:
...
articles: [{type:ObjectId, ref:'Article'}]
文章模型包含对帐户模型
的引用。
Article:
...
account: {type:ObjectId, ref:'Account'}
因此,使用填充的 articles
类别模型将是:
{ //category
articles: //this field is populated
[ { account: 52386c14fbb3e9ef28000001, // I want this field to be populated
date: Fri Sep 20 2013 00:00:00 GMT+0400 (MSK),
title: 'Article 1' } ],
title: 'Category 1' }
问题是:如何填充已填充字段([articles])的子字段(帐户)?这是我现在的做法:
globals.models.Category
.find
issue : req.params.id
null
sort:
order: 1
.populate("articles") # this populates only article field, article.account is not populated
.exec (err, categories) ->
console.log categories
我知道这里讨论过:Mongoose: Populate a populated field但没有找到真正的解决方案
最佳答案
首先,将 mongoose 3 更新为 4,然后使用最简单的方法在 mongoose 中进行深度填充,如下所示:
假设您的博客架构具有 userId 作为 ref Id,然后在 User 中您有一些评论作为架构 Review 的 ref Id。所以基本上,你有三个模式:
而且,您必须从博客中查询,哪个用户拥有此博客和用户评论。 所以你可以查询你的结果:
BlogModel
.find()
.populate({
path : 'userId',
populate : {
path : 'reviewId'
}
})
.exec(function (err, res) {
})
https://stackoverflow.com/questions/18867628/