mongodb json验证在独立mongo中不起作用

时间:2019-05-14 13:12:50

标签: mongodb validation json-schema-validator

我有独立的mongo设置,我添加了具有所有验证的collections脚本,但验证不起作用..因此尝试了此mongo guide,并执行了类似的步骤,但验证不起作用。它正在接受文件。

我在收集脚本下面执行了

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "name", "year", "major", "gpa", "address.city", "address.street" ],
         properties: {
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            },
            gender: {
               bsonType: "string",
               description: "must be a string and is not required"
            },
            year: {
               bsonType: "int",
               minimum: 2017,
               maximum: 3017,
               exclusiveMaximum: false,
               description: "must be an integer in [ 2017, 3017 ] and is required"
            },
            major: {
               enum: [ "Math", "English", "Computer Science", "History", null ],
               description: "can only be one of the enum values and is required"
            },
            gpa: {
               bsonType: [ "double" ],
               description: "must be a double and is required"
            },
            "address.city" : {
               bsonType: "string",
               description: "must be a string and is required"
            },
            "address.street" : {
               bsonType: "string",
               description: "must be a string and is required"
            }
         }
      }
   },
   validationLevel: "strict",
validationAction: "error"
})

然后在下面的查询中执行。

db.students.insert({
   name: "Alice",
   year: NumberInt(2019),
   major: "History",
   gpa: NumberInt(3),
   address: {
      city: "NYC",
      street: "33rd Street"
   }
})

得到以下答复

WriteResult({ "nInserted" : 1 })

但是我期望验证错误。

WriteResult({
   "nInserted" : 0,
   "writeError" : {
      "code" : 121,
      "errmsg" : "Document failed validation"
   }
})

我需要在mongodb设置中启用某些功能吗?

我的mongo版本是

MongoDB shell version v3.4.18

1 个答案:

答案 0 :(得分:2)

您需要将“gpa:NumberInt(3)”行替换为“gpa:3”

= IFERROR( VLOOKUP( [@[Client Name]], lo.DATA,
 MATCH( AU$11, lo.DATA[#Headers], 0 ), 0 ), "" )

我希望它有效