我尝试为存储在子文档的属性中的对象ID创建动态引用。该属性可以引用多个模型(甚至是在不同mongoose数据库示例中注册的模型),因此我直接使用模型示例而不是模型名称。
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)
字符串
正如你所看到的,ref
属性是来自CategoryAModel
或CategoryBModel
的文档的Object ID。我开始为这个模型创建文档,如下所示:
const data = {
name: 'Test Data',
children: [
{
category: 'catA',
ref: '658e9f1901f3da2c14920401' // <- existing document from CategoryAModel
},
{
category: 'catB',
ref: '654995e0c89d1c19c84e77b7' // <- existing document from CategoryBModel
}
]
}
型
但是当我尝试populate
时,category: 'catA'
的ref
变为null。我在ref
的ref函数中记录了this
上下文,发现this
引用了正在处理的文档(与上面的数据相同的形状),并且this.category
是未定义的,因为它实际上在children
数组中。本质上,使ref
始终成为CategoryBModel
既然它是一个数组,我该怎么做一个动态引用呢?有没有办法访问被引用的subSchema
的索引呢?
1条答案
按热度按时间3yhwsihp1#
您应该使用
refPath
。Mongoose设计了populate
方法,将refPath
选项用于动态模型,并将满足您的需求。像这样使用它:
字符串
现在当你保存你的
Parent
文档时,children
对象应该有你想要填充的模型存储在那个classifier
字符串中。你的数据看起来像这样:型
现在你可以像这样填充:
型