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
});
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?
You can always do:
var Script = mongoose.model('Script');
Person.findOne({ name: "some name" }, function(err, person) {
Script.find({ _creator: person._id }, function(err, scripts) {
person.scripts = scripts;
...
})
})
Hope that might helped.