【问题标题】:MongoDB validation part with any one of the field check带有任何一项字段检查的 MongoDB 验证部分
【发布时间】:2021-12-23 06:03:29
【问题描述】:

试图弄清楚如何验证对象数组和映射(键,值)对

{
    "commonIdentification": {
        "CR": "BR",
        "SN": "NAVS87397394"
    }
    "digitalIdentiifcation": {
        "UUID": "326f040b-cf14-4cf9-9e67-57f7ca3ce1b2"
    }
}

在这里,我想添加验证,

  1. 通常识别存在 CR,不需要数字识别的存在
  2. 在没有 CR 的常见标识中,需要 digitalIdentiifcation 的“UUID”字段。

任何人都可以帮助重新格式化验证部分吗?我正在努力根据上述几点进行验证。

{
  $jsonSchema: {
    properties: {
      commonIdentification: {
        type: 'object',
        required: [
          'CR'
        ],
        properties: {
          CR: {
            type: 'string',
            description: 'CR is mandatory '
          }
        }
      }
    },
    type: 'object',
    required: [
      'commonIdentification',
      'digitalIdentiifcation'
    ]
  }
}

【问题讨论】:

    标签: mongodb validation mongodb-query


    【解决方案1】:
    {
     $jsonSchema: {
    type: 'object',
    required: [
      'commonIdentification'
    ],
    anyOf: [
      {
        required: [
          'digitalIdentiifcation'
        ]
      },
      {
        required: [
          'commonIdentification'
        ]
      }
    ],
    properties: {
      commonIdentification: {
        type: 'object',
        anyOf: [
          {
            required: [
              'CR'
            ]
          },
          {
            required: [
              'SN'
            ]
          }
        ],
        properties: {
          SN: {
            type: 'string',
            description: 'SN or CR or digitalIdentiifcation is mandatory '
          },
          CR: {
            type: 'string',
            description: 'SN or CR or digitalIdentiifcation is mandatory '
          }
        },
        description: 'SN or CR or digitalIdentiifcation is mandatory '
      },
      digitalIdentiifcation: {
        description: 'digitalIdentiifcation'
      }
    }
    

    } }

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2023-03-20
    • 2017-01-22
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2017-10-30
    • 2019-11-09
    相关资源
    最近更新 更多