指南针中的MongoDB $ jsonSchema验证错误-“未知的$ jsonSchema关键字”

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

由于将几何图形用作键,因此不确定为什么会显示此错误。

请问我如何纠正此问题的任何建议?

{
  $jsonSchema: {
    bsonType: "object",required: ["properties.Name","properties.Country","geometry.type","geometry.coordinates"],properties:{
      Country: {
        bsonType: "string",description: "Must be supplied"
      },Name: {
        bsonType: "string",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"
    }
  }
}
beida221 回答:指南针中的MongoDB $ jsonSchema验证错误-“未知的$ jsonSchema关键字”

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

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

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

下面的代码在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: {
        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/

本文链接:https://www.f2er.com/2787496.html

大家都在问