我已经以编程方式创建了一个包含一些具有枚举值的字段的集合,但是,当我尝试在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
},
},
},
},
});