我想使用 JSON schema 文件创建 MongoDB 集合。
假设 JSON 文件 address.schema.json
包含地址信息模式(此文件是 Json-schema.org 的示例之一):
{
"$id": "https://example.com/address.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "An address similar to http://microformats.org/wiki/h-card",
"type": "object",
"properties": {
"post-office-box": {
"type": "string"
},
"extended-address": {
"type": "string"
},
"street-address": {
"type": "string"
},
"locality": {
"type": "string"
},
"region": {
"type": "string"
},
"postal-code": {
"type": "string"
},
"country-name": {
"type": "string"
}
},
"required": [ "locality", "region", "country-name" ],
"dependencies": {
"post-office-box": [ "street-address" ],
"extended-address": [ "street-address" ]
}
}
如何使用MongoDB命令创建一个基于上述模式的MongoDB集合, 如 mongoimport 或者 db.createCollection?
如果可以直接在MongoDB中使用文件,而无需手动更改文件格式,那将很方便。
我想知道JSON模式格式是否是标准的,为什么我需要更改它才能适应MongoDB。为什么MongoDB没有内置此功能?
bsonType
不等于jsonType
,因此可用选项可能会有所不同。但请参见此处的注释(MongoDB支持JSON模式的草案4
):https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#op._S_jsonSchema。不确定它的意思,但您可以检查一下,也许您的模式也可以工作。 - dododo