javascript - 不是有效的收集选项,mongodb
问题描述
我正在尝试在 MongoDB 中创建一个集合,但出现错误:
“名称”字段不是有效的集合选项
我正在使用 MongoDB 版本 4.0.0。编码:
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/newApp', (err, db) =>{
if (err) {
return console.log('mongodb connection error');
}
db.createCollection('people', {
name: 'sarah',
age: 27,
location: 'honolului',
relationship: 'unknown'
}, (err, result) => {
if (err) {
return console.log('query has not successed retry error code ' + err);
}
conole.log(result.ops);
});
});
解决方案
您以错误的方式使用它,您必须像这样使用它:
如需更多帮助:
https://docs.mongodb.com/manual/reference/method/db.createCollection/
db.createCollection( "people", {
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
age: {
bsonType : "number",
description: "must be a number and match the regular expression pattern"
},
location: {
bsonType: "string",
description: "must be a string"
},
relationship: {
bsonType: "string",
description: "can only be one of the enum values"
}
}
} }
} )
注意:验证器将为您提供应用自定义验证方法的选项,例如required
或unique
。
推荐阅读
- php - 有时无法从 codeigniter 中的数据库获取响应
- python - 如何将目录中的图像转换为np数组
- javascript - 如何从内部函数访问 Firebase 托管项目目标 ID
- ios - 更改 NSManagedObject 属性的值不会反映在 DiffableDataSource
- freertos - FreeRTOS - 为最后一次函数调用恢复堆栈指针
- java - 用于组合单独子阵列中的单个元素的算法
- graphql - GraphQL 中的动态类型
- android - 带有动态图像 URL 的 Android 谷歌地图集群项目标记 - 用所有标记覆盖 URL
- multithreading - Rust 的通道如何实现?
- ansible - 在多个清单主机组上运行 ansible 任务