数组的模式"/>
Mongoose:对象数组的模式
我正在尝试为一组对象创建猫鼬模式。我尝试了所有可能的参考资料,但我仍然看不到它已保存到数据库中。
这是我正在处理的对象结构:
{
"incidentNumber": "INC12345",
"resource": [
"string"
],
"actionsItems": {
"detection": [
{
"key": "string",
"value": "string"
}
],
"prevention": [
{
"key": "string",
"value": "string"
}
],
"mitigation": [
{
"key": "string",
"value": "string"
}
],
"tasks": [
{
"key": "string",
"value": "string"
}
]
}
}
参考官方文档创建了这样的mongoose schema:
import * as mongoose from 'mongoose';
const ItemsSchema = new mongoose.Schema({
key: { type: String, required: false },
value: { type: String, required: false },
});
const ActionItemSchema = new mongoose.Schema({
detection: [ItemsSchema],
prevention: [ItemsSchema],
mitigation: [ItemsSchema],
task: [ItemsSchema],
});
export const IncidentSchema = new mongoose.Schema(
{
incidentNumber: {
type: String,
required: true,
},
resource: [
{
type: String,
required: true,
},
],
actionItems: ActionItemSchema,
},
{
collection: 'incidents',
},
);
此模式结构的问题在于,无论我传递什么作为值,它都会在 actionsItems 下创建空数组。 resource 数组已正确保留。附上截图供参考。
我在这里错过了什么吗? TIA.
回答如下:更多推荐
Mongoose:对象数组的模式
发布评论