猫鼬:在父级中没有引用的情况下填充 [英] Mongoose: Populating without ref in parent
问题描述
在我的node.js +猫鼬应用程序中,我有一个父级和一个子级架构,它们彼此具有引用:
In my node.js + mongoose application I have a parent and a child schema which have the refs of each other:
var PersonSchema = new Schema({
name : String
, age : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : { type: Schema.ObjectId, ref: 'Person' }
, title : String
, fans : [{ type: Schema.ObjectId, ref: 'Person' }]
});
现在,我可以通过以下方式填充所有故事:
Now I am able to populate all stories when fetching a person by:
Person.findOne({ name: "some name"}).populate('stories').exec(...);
最近,我意识到面对面不断增长的故事(例如故事)会降低巨大数组的性能.所以我想改变一下,因为在我的情况下,阵列的增长非常快并且变得庞大.我的解决方案似乎很简单.我将人员模式更改为
Recently I recognized that a growing array like stories in person slows down the performance for huge arrays. So I want to change that, because in my case the array is growing really fast and gets huge. My solution appeared to be easy. I changed the person schema to
var PersonSchema = new Schema({
name : String
, age : Number
});
只有StorySchema保留引用.
that only the StorySchema holds the ref.
现在的问题:即使是我个人的故事,也有办法或解决方法吗?
Now the question: Is there a way or workaround to populate even so the stories in my person?
推荐答案
您可以随时这样做:
var Script = mongoose.model('Script');
Person.findOne({ name: "some name" }, function(err, person) {
Script.find({ _creator: person._id }, function(err, scripts) {
person.scripts = scripts;
...
})
})
希望有帮助.
这篇关于猫鼬:在父级中没有引用的情况下填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!