I'm trying to create a dynamic ref for an object ID stored inside a property of a subdocument. The property can reference multiple models (even models registered in a different mongoose database instance) so I use the model instance directly instead of a model name.
const subSchema = new Schema({
category: String,
ref: {
type: Schema.Types.ObjectId,
ref: function() {
return this.category === 'catA' ? CategoryAModel : CategoryBModel // imports model instances
}
}
})
const parentSchema = new Schema({
name: String,
children: [subSchema]
})
const ParentModel = db.model('Parent', parentSchema)
As you can see, the ref property is the Object ID of either a document from CategoryAModel or CategoryBModel. I started creating documents for this model, such as the following:
const data = {
name: 'Test Data',
children: [
{
category: 'catA',
ref: '658e9f1901f3da2c14920401' // <- existing document from CategoryAModel
},
{
category: 'catB',
ref: '654995e0c89d1c19c84e77b7' // <- existing document from CategoryBModel
}
]
}
But when i tried to populate this, the ref for category: 'catA' becomes null (despite existing). I logged the this context in the ref's ref function and saw that this refers to the document being processed (same shape as the data above), and that this.category is undefined because it's actually inside the children array. Essentially making the ref always result in being the CategoryBModel
Since it's an array, how would I go and make a dynamic reference? Is there a way to access the index of the subSchema being referred to?
You should use
refPathinstead. Mongoose has designed thepopulatemethod to use therefPathoption for dynamic models and will meet your needs.Use it like so:
Now when you save your
Parentdocuments thechildrenobjects should have which model you want to populate with stored in thatclassifierstring. Your data will look like:Now you can populate like so: