Mongoose:对象数组的模式

编程入门 行业动态 更新时间:2024-10-04 11:20:07

Mongoose:对象<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组的模式"/>

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:对象数组的模式

本文发布于:2024-05-30 19:14:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   模式   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!