【问题标题】:MongoDB $jsonSchema Validation Error in Compass - "Unknown $jsonSchema keyword"Compass 中的 MongoDB $jsonSchema 验证错误 - “未知的 $jsonSchema 关键字”
【发布时间】:2020-01-11 02:15:36
【问题描述】:

我正在使用 Compass 的验证选项卡手动输入下面的 $jsonSchema 验证。 不幸的是,它一直显示错误“未知 $jsonSchema 关键字:几何”

不知道为什么会出现这个错误,因为几何被用作键。

请告诉我如何纠正这个问题?

{
  $jsonSchema: {
    bsonType: "object",
    required: ["properties.Name", "properties.Country", "geometry.type", "geometry.coordinates"],
    properties:{
      Country: {
        bsonType: "string",
        description: "Must be supplied"
      },
      Name: {
        bsonType: "string",
        description: "Must be supplied"
      },
      description: {
        bsonType: "string",
        description: "Optional description"
      }
    },
    geometry: {
      type: {
        bsonType: "string",
        enum: ["Point"],
        description: "Must be Point"
      },
      coordinates: {
        bsonType: ["double"],
        description: "Longitude, Latitude"
      }
    },
    datePosted: {
      bsonType: "date",
      description: "Auto-added field"
    },
    image: {
      bsonType: "string",
      description: "URL of image location"
    }
  }
}

【问题讨论】:

  • 检查您的 MongoDB 版本是否 >= 3.6
  • 是的,服务器版本是4.2.2

标签: mongodb jsonschema mongodb-compass


【解决方案1】:

您提供的 JSON 架构看起来不太正确。关于未知“几何”关键字的错误是因为该属性应该描述您的属性之一。 JSON Schema 文件的结构是严格的,并且遵循严格的(但令人困惑的)规范。

我发现您提供的架构有一些问题:

  1. required 属性数组应列出 properties 对象。所以在你的情况下,它应该是这样的 required: ["Name", "Country", "geometry"]
  2. geometrydatePostedimage 对象需要放在 properties 对象内。
  3. geometry 对象的描述本身必须是另一个 JSON Schema(它是一种递归模式)。
  4. 什么是几何类型?您已将其定义为字符串和仅具有一个可能选项(“Point”)的枚举。枚举仅在您提供多个选项时才有意义,并且它们的值将取代指定的数据类型。

以下代码在 MongoDB Compass 上测试:

{
  $jsonSchema: {
    bsonType: 'object',
    required: [
      'properties.Name',
      'properties.Country',
      'geometry.type',
      'geometry.coordinates'
    ],
    properties: {
      Country: {
        bsonType: 'string',
        description: 'Must be supplied'
      },
      Name: {
        bsonType: 'string',
        description: 'Must be supplied'
      },
      description: {
        bsonType: 'string',
        description: 'Optional description'
      },
      geometry: {
        type: 'object',
        properties: {
          type: {
            'enum': [
              'Point'
            ],
            description: 'Must be Point'
          },
          coordinates: {
            bsonType: [
              'object'
            ],
            description: 'Contains Longitude, Latitude',
            properties: {
              longitude: {
                type: 'number',
                description: 'Decimal representation of longitude'
              },
              latitude: {
                type: 'number',
                description: 'Decimal representation of latitude'
              }
            }
          }
        }
      },
      datePosted: {
        bsonType: 'date',
        description: 'Auto-added field'
      },
      image: {
        bsonType: 'string',
        description: 'URL of image location'
      }
    }
  }
}

查看文档中的示例:https://docs.mongodb.com/manual/core/schema-validation/

【讨论】:

    【解决方案2】:

    geometry 是 innerjson 对象,所以你需要说明这个 innerjson 对象的类型 几何学:{ bsonType:'object'} 然后提及必填字段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多