Mongodb Mongo Json模式验证程序AnyOf不工作

Mongodb Mongo Json模式验证程序AnyOf不工作,mongodb,jsonschema,json-schema-validator,Mongodb,Jsonschema,Json Schema Validator,我已创建具有以下验证的a集合: { $jsonSchema: { bsonType: 'object', additionalProperties: false, properties: { _id: { bsonType: 'objectId' }, test: { bsonType: 'string' } }, anyOf: [ { bson

我已创建具有以下验证的a集合:

{
  $jsonSchema: {
    bsonType: 'object',
    additionalProperties: false,
    properties: {
      _id: {
        bsonType: 'objectId'
      },
      test: {
        bsonType: 'string'
      }
    },
    anyOf: [
      {
        bsonType: 'object',
        properties: {
          test1: {
            bsonType: 'string'
          }
        },
        additionalProperties: false
      },
      {
        bsonType: 'object',
        properties: {
          test2: {
            bsonType: 'string'
          }
        },
        additionalProperties: false
      }
    ]
  }
}
验证操作设置为错误,验证级别设置为中等

当我尝试插入一个包含字段testtest1的文档时,我得到了一个验证错误

Document:
db.dmt9.insert([{test:"123"},{test1:"456"}])

Error:

BulkWriteResult({
        "writeErrors" : [
                {
                        "index" : 0,
                        "code" : 121,
                        "errmsg" : "Document failed validation",
                        "op" : {
                                "_id" : ObjectId("5dd511de1f7b2047ed527044"),
                                "test" : "123"
                        }
                }
        ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
既然我正在插入一个包含AnyOf中的一个字段的文档,那么该文档不应该成功插入吗?如果我尝试将验证操作更改为警告,则会插入文档,但我可以插入任何其他字段

Test with validation Action - Warning

rs0:PRIMARY> db.dmt9.insert([{test:"123"},{test1:"456"},{test9:123}])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
更新2: 当我移除现场测试时,我得到了这个验证器:

{
  $jsonSchema: {
    bsonType: 'object',
    additionalProperties: false,
    anyOf: [
      {
        bsonType: 'object',
        properties: {
          test1: {
            bsonType: 'string'
          }
        },
        additionalProperties: false
      },
      {
        bsonType: 'object',
        properties: {
          test2: {
            bsonType: 'string'
          }
        },
        additionalProperties: false
      }
    ],
    properties: {
      _id: {
        bsonType: 'objectId'
      }
    }
  }
}
当我再次尝试插入test1时,仍然会收到错误消息Document validation failed(文档验证失败)

rs0:PRIMARY> db.dmt8.insert([{test1:"123"}])
BulkWriteResult({
        "writeErrors" : [
                {
                        "index" : 0,
                        "code" : 121,
                        "errmsg" : "Document failed validation",
                        "op" : {
                                "_id" : ObjectId("5dd51b4766ba25a01fbcf8e6"),
                                "test1" : "123"
                        }
                }
        ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
{test:“123”}
验证失败,因为它不符合
anyOf
中的任何模式,该模式需要
test1
test2
作为唯一键

anyOf
将每个子模式应用于您的实例,并在至少一个子模式通过验证时断言有效

{test1:“123”}
失败,因为根架构
附加属性:false
阻止对象中未在同一架构对象
属性
模式属性
中定义的任何键

解决办法是有一些重复

在示例中(链接用于浏览器内测试,但仅限于draft-7),我添加了根属性
test1
test2
。这将允许密钥为
test1
test2
的数据通过,但鉴于我不知道您的要求,我无法告诉您如何修改模式以允许密钥为
test
的对象通过(因为每个
anyOf
子模式都阻止它)

如果您的目的是检查插入的内容之一是否具有
test1
test2
,那么JSON模式恐怕无法帮助您。Mongo上下文中的JSON模式只能单独检查每个项,不能验证可能插入的记录集合


在上面的模式示例中,我删除了类型检查,因为这与此问题无关,而且bsonType与JSON模式类型不同。

您认为
anyOf
应该做什么?我希望anyOf允许我插入一个包含字段“test1”或“test2”的文档,谢谢您的回答。我试图删除字段测试并插入一个只有一个字段“test1”的文档,但是它仍然给我一个错误的验证错误:``定义“错误的验证错误”?您能否更新您的问题以显示此附加测试数据、您得到的结果以及您期望的结果好的,我明白问题所在了。我将在大约一小时内向您展示解决方案。感谢您的投入:)期待您的反馈:)非常感谢您的帮助。非常感谢。
{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "_id": {},
    "test": {},
    "test1": {},
    "test2": {}
  },
  "anyOf": [
    {
      "type": "object",
      "properties": {
        "test1": {}
      },
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "test2": {}
      },
      "additionalProperties": false
    }
  ]
}