我有3个模型model1
model2
model3
,它的架构看起来像这样-
const model1Schema = new Schema ({
//dummy data
_model2SchemaAssociated: [{type: mongoose.Schema.Types.ObjectId ref:'model2'}],
_model3SchemaAssociated: [{type: mongoose.Schema.Types.ObjectId ref:'model3'}]
});
const model2Schema = new Schema ({
//dummy data
_model1SchemaAssociated: [{type: mongoose.Schema.Types.ObjectId ref:'model1'}],
_model3SchemaAssociated: [{type: mongoose.Schema.Types.ObjectId ref:'model3'}]
});
const model3Schema = new Schema ({
//dummy data
_model1SchemaAssociated: [{type: mongoose.Schema.Types.ObjectId ref:'model1'}],
_model2SchemaAssociated: [{type: mongoose.Schema.Types.ObjectId ref:'model2'}]
});
无论何时将新文档添加到上述任何模型中,我都想更新associatedSchemas
。
我的解决方案 只要保存了任何架构,就查找其他2个模型。并更新当前架构
但是我觉得这是一个效率低下的操作。
有没有更好的方法来解决这个问题?