const FormFieldsSchema = new Schema({
formName: {
type: String,
required: true
},
fields: [
{
fieldLabel: {
type: String,
required: true
},
inputData: [{
type: mongoose.Schema.ObjectId,
ref: "UserData"
}]
}
]
});
大家好,我有一个模式,其中 fields 是一个数组,现在对于每个数组项,我都有一个 _id 属性,我希望找到该数组并更新它,但我无法做到这一点,
我尝试了 findByIdAndUpdate
,但它不起作用,还尝试了 parent.inputData.id(_id);
还没有运气,有什么想法吗?
最佳答案
将数组的内容转换为单独的模型,然后您可以直接查询该模型。
在FormFieldsSchema
上运行findByIdAndUpdate
填充搜索FormFields
的_id
而不是字段的内容
数组。
const FieldSchema = new Schema({
fieldLabel: {
type: String,
required: true
},
inputData: [{
type: mongoose.Schema.ObjectId,
ref: "UserData"
}]
})
并将field
更改为fields: [FieldSchema]
另外,不要忘记将 FieldSchema
导出为模型。
关于javascript - 通过 id 在 schema 中搜索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60476136/