Javascript jsonschema mongodb未从.js文件进行验证

Javascript jsonschema mongodb未从.js文件进行验证,javascript,mongodb,mongodb-query,Javascript,Mongodb,Mongodb Query,我有mongo.js命令来创建集合,在我的.js文件中: let organizationSchema = { bsonType: "object", properties: { name: { bsonType: "string", description: "must be a string and required" },

我有
mongo.js
命令来创建集合,在我的
.js
文件中:

let organizationSchema = {
    bsonType: "object",
    properties: {
        name: {
            bsonType: "string",
            description: "must be a string and required"
        },
        active: {
            bsonType: "bool",
        },
        location: {
            bsonType: "string",
        },
        contactInfo: {
            // this one doest working for validate
            pattern: "/^\+?\d+(?:-\d+)*(?:\(\d+\)-\d+)*$/"
        },
        createdBy: {
            bsonType: "string",
        },
        createdAt: {
            bsonType: "date",
        },
        updatedAt: {
            bsonType: "date",
        },
        deletedAt: {
            bsonType: "date",
        },
    }
}
db.runCommand( { collMod: "organizations", validator:{ $jsonSchema: organizationSchema }, validationLevel: "strict"} )
db.organizations.createIndex( { "name": 1, "createdBy": 1, "location": 1, "active": 1} )
然后我运行
mongo”mongodb+srv://url“--用户名dev-p'dev'

contactInfo
上,我对验证电话号码执行模式,但当我在MongoDB客户端上插入数据时,插入的电话号码无效,我的
js
上的语法是否有问题,我应该在每个键上使用字符串

const r=/^\+?\d+(?:-\d+)*(?:\(\d+)-\d+*$/;
控制台日志(r.test(“+1110988888”))

console.log(r.test(“zzzzuuuu”)
可能是您还应该指定
bsonType:“string”
。我也这样做了,但不起作用@prasad_contactInfo的正则表达式无效,只需检查获取此错误
正则表达式无效:无需重复
,尝试此模式即可
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
你是对的,我上面的正则表达式在javascript中是有效的,我刚刚从javascript复制了它,有没有让正则表达式工作的选项@turivishali没有更多关于正则表达式的知识,您能否分享您对模式的确切要求,例如
123-1234-1234
可能是您还应该指定
b类型:“string”
。我也这样做了,但@prasad_正则表达式对于
contactInfo
无效,刚刚检查获取此错误
正则表达式无效:无需重复
,尝试此模式它将工作
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
您是对的,我上面的正则表达式在javascript中是有效的,我刚刚从javascript复制了它,这是使正则表达式工作的任何选项吗@turivishali没有更多关于正则表达式的知识,您能否分享您对模式的确切要求,例如
123-1234-1234