MongoDB $jsonSchema指南针中的验证错误 - "Unknown $jsonSchema keyword"



我正在使用指南针的验证选项卡手动输入下面的$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"
}
}
}

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

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

  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: '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/

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

最新更新