【问题标题】:is it possible to use if then condition in mongo db validation?是否可以在 mongo db 验证中使用 if then 条件?
【发布时间】:2020-07-07 15:20:44
【问题描述】:

在 mongo db compass 上,我想使用验证模式,但我的属性中有一个是枚举,取决于这个字段,另一个字段可以切换我可以使用 JSON 模式来做到这一点,但它似乎没有为什么要在 mongoDB compass 上工作?

{
  type: 'object',
  properties: {
    type: {
      enum: ['teacher', 'student']
    },
    firstname: {
      type: 'string',
    },
    lastname: {
      type: 'string',
    },
    login: {
      type: 'string',
    },
    pwd: {
      type: 'string'
    },
    "if": {"properties": {"type": {"const": "student"}}},
    "then": {"properties": {classes: {type: "array"}}}
  },
  required: [
    'type',
    'firstname',
    'lastname',
    'login',
    'pwd',
  ],
};

【问题讨论】:

    标签: mongodb json-schema-validator mongodb-compass


    【解决方案1】:

    没有。

    MongoDB documentation 表示它正在使用草案 4:

    JSON Schema object is formatted according to draft 4 of the JSON Schema standard.
    

    JSON Schema 网站说这些条件是草案 7 中的新内容:

    New in draft 7 if, then and else keywords
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2011-05-19
      相关资源
      最近更新 更多