猫鼬:深度人口(填充人口稠密的领域) [英] Mongoose: deep population (populate a populated field)
本文介绍了猫鼬:深度人口(填充人口稠密的领域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 Category
模型:
Category:
...
articles: [{type:ObjectId, ref:'Article'}]
文章模型包含对账户模型
的引用.
Article model contains ref to Account model
.
Article:
...
account: {type:ObjectId, ref:'Account'}
因此,填充了 articles
的类别模型将是:
So, with populated articles
Category model will be:
{ //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' }
问题是:如何填充已填充字段([文章])的子字段(帐户)?这是我现在的做法:
The questions is: how to populate subfield (account) of a populated field ([articles])? Here is how I do it now:
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
我知道这里讨论过:猫鼬:填充填充字段但没有找到了真正的解决方案
I know it was discussed here: Mongoose: Populate a populated field but no real solution was found
推荐答案
Mongoose 现在有一个新的方法 Model.populate
用于深度关联:
Mongoose has now a new method Model.populate
for deep associations:
https://github.com/Automattic/mongoose/issues/1377#issuecomment-15911192
这篇关于猫鼬:深度人口(填充人口稠密的领域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文