Imagine that exists such mongoose schema:
BaseSchema({
value: String
});
And also exists some discriminated schemas:
ChildOneSchema({
items: [{
type: ObejctId, ref: 'ChildOneItem'
}]
})
ChildTwoSchema({
items2: [{
type: ObejctId, ref: 'ChildTwoItem'
}]
})
And so on. Each child schema can have different field count, field names ect. The only common thing - that all of them are stored at the BaseSchemaCollection.
BaseSchema are using in other collection:
OtherSchema({
items: [{
type: ObejctId, ref: 'BaseSchema'
}]
})
For example we want to query by OtherSchema to get the OtherSchemaDocument with populated items array:
OtherModel
.find({_id: someId})
.populate('items')
.exec(callback)
But what if we want to populate not only the 'items' of OtherSchema, but also we want to populate fields that belong to children schemas too. Yeah, we can do something like that:
OtherModel
.find({_id: someId})
.populate({
path: 'items',
populate: {
{
path: 'items'
},
{
path: 'items2'
}
}
})
.exec(callback)
But if child schemas has different fields, or we got more child schemas this populate construction grows(imagine this with match, sort, select options). So, the question is: how to avoid such populate construcions(may be there is a way to find out actual type of document in one call and use pre hooks to make populate logic for each child schema) or how to change schemas to still be able query by the BaseSchema to get same results?
via irisk
No comments:
Post a Comment