javascript - 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
},
},
},
},
});
解决方案
推荐阅读
- abap - 无法访问语句
- oracle - Oracle Apex - 通过 URL 创建/传递/设置交互式报表 (IR) 过滤器作为参数
- go - 由于扩展字段错误,proto 文件无法编译
- google-cloud-platform - AI 平台内置图像分类算法不会在训练结束时导出模型
- r - 创建一个带有循环方差分析结果的表 r
- android - 使用 VolleyMultipart 请求在文件上传期间显示进度
- reactjs - 设置状态时如何将方法应用于字符串,反应钩子
- python-3.x - PyQt5让QDialog在允许用户输入之前等待进程完成
- sorting - DAX Related() 与 Power BI 中的多对多关系
- c# - 使用 .net 核心 json 模型绑定在无效 json 上引发 json 验证错误