我有两个型号,它们是(表单,用户(
每个用户都有formId字段,该字段是他拥有的表单对象的对象id
每个表单都有一个名为userId的字段,该字段用于拥有该表单的用户
问题是,我使用的是mongoose自动填充包,如果我将formId和userId字段更改为自动填充,它将打开一个没有结束的无限填充
问题是,一些其他模式有一个名为formId的字段,它与表单对象相关联,在这种情况下,我想填充userId字段以获得用户信息,而不需要再次填充formId字段。
架构设计
const userSchema = new Schema({
name: String,
age: Number,
formId: {
type: Schema.Types.ObjectId,
ref: 'Form',
//this value used to auto populate for mongoose-autopopulate package
autopopulate: true,
},
})
//signing the mongoose-autopopulate plugin
...
//exporting the module
---------------
const formSchema = new Schema({
address: String,
bill: Number,
//the id of the user who added this form
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
//this value used to auto populate for mongoose-autopopulate package
autopopulate: true,
},
})
//signing the mongoose-autopopulate plugin
...
//exporting the module
----------
const orderSchema = new Schema({
products: [],
deliveryBoy: {
type: Schema.Types.ObjectId,
ref: 'Boy',
},
formId: {
type: Schema.Types.ObjectId,
ref: 'Form',
//this value used to auto populate for mongoose-autopopulate package
autopopulate: true,
},
})
//signing the mongoose-autopopulate plugin
...
//exporting the module
谢谢,
使用maxDepth选项猫鼬自动填充
例如
const orderSchema = new Schema({
products: [],
deliveryBoy: {
type: Schema.Types.ObjectId,
ref: 'Boy',
},
formId: {
type: Schema.Types.ObjectId,
ref: 'Form',
autopopulate: { maxDepth: 2 },
},
})