首页 > 解决方案 > MongoDB - 将数据插入包含枚举的集合时出现文档验证失败错误

问题描述

我以编程方式创建了一个集合,其中包含一些具有枚举值的字段,但是,当我尝试手动将一些数据插入到 MongoDB Atlas 站点上的集合中时,我会收到文档验证失败错误。

我如何使用此处插入数据的屏幕截图: MongoDB Atlas

还参考屏幕截图,我尝试将类型更改为array并将第一个元素设置为我所说的枚举类型之一。

代码

await db.createCollection("components", {
      autoIndexId: true,
      validator: {
        bsonType: "object",
        $jsonSchema: {
          required: ["name", "type", "brand", "qty", "price"],
          properties: {
            name: {
              bsonType: "string",
              description: "must be a string and is required",
            },
            type: {
              enum: [
                "Motherboard",
                "Processor",
                "Graphics Card",
                "Memory",
                "Storage",
                "Power Supply",
                "Operating System",
              ],
            },
            manufacturer: {
              enum: ["Nvidia", "AMD", "Intel"],
            },
            brand: {
              enum: [
                "ASUS",
                "Gigabyte",
                "Zotac",
                "MSI",
                "EVGA",
                "Sapphire",
                "Palit",
              ],
            },
            description: {
              bsonType: "string",
            },
            qty: {
              bsonType: "int",
            },
            price: {
              bsonType: "double", // review type
            },
          },
        },
      },
    });

标签: javascriptmongodb

解决方案


推荐阅读