首页 > 解决方案 > 是否可以在 mongo db 验证中使用 if then 条件?

问题描述

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

{
  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',
  ],
};

标签: mongodbjson-schema-validatormongodb-compass

解决方案


不。

MongoDB文档表明它正在使用草案 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

推荐阅读