最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Mongoose:对象数组的模式

网站源码admin36浏览0评论

Mongoose:对象数组的模式

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.

回答如下:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论