我如何使用 Mongo 中的 Json 模式验证 Json 文件?
How do i validate a Json file using Json schema in Mongo?
我有一个 JSON 文件,我想使用 MongoDB 上的 JSON 架构验证该文件。
我如何将 JSON 架构导入 MongoDB,然后验证 JSON 文件。
JSON 文件已经在一个集合中,所以我想在不导入新的 JSON 文件的情况下进行验证。
JSON:
{
"Book": {
"Year:2016-2017": {
"Crs": [{
"Cr": {
"_id": {
"$oid": "5a439ff4fc0900f06fb470a4"
},
"Number": 35,
"Pag": 8,
"Desc": "Embl",
"Ad": "S",
"Type": "Embl"
}
}]
}
}
}
JSON 架构:
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required": false,
"properties": {"Book": {
"type": "object",
"id": "http://jsonschema.net/Book",
"required": false,
"properties": {"Year:2016-2017": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017",
"required": false,
"properties": {"Crs": {
"type": "array",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs",
"required": false,
"items": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0",
"required": false,
"properties": {"Cr": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr",
"required": false,
"properties": {
"Ad": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Ad",
"required": false
},
"Desc": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Desc",
"required": false
},
"Number": {
"type": "number",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Number",
"required": false
},
"Pag": {
"type": "number",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Pag",
"required": false
},
"Type": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Type",
"required": false
},
"_id": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/_id",
"required": false,
"properties": {"$oid": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/_id/$oid",
"required": false
}}
}
}
}}
}
}}
}}
}}}
我想使用 mongodb 的模式验证 JSON。
JSON file is already in one collection so i want to validate without importing a new JSON file.
从 MongoDB v3.6.x 开始,document schema validation 仅在更新和插入期间发生,现有文档在修改之前不会进行验证检查。这意味着您在 MongoDB 集合中的现有 JSON 文档将不会通过您刚刚应用的验证规则进行验证。
请注意 $jsonSchema operator and JSON-SCHEMA 支持是 MongoDB 版本 3.6 中的新增功能。
i want to add a new "Cr" but it needs to respect the JSON Schema.(Insertion/Update)
在集合上指定文档架构验证后,任何更新操作都会触发对受影响文档的验证检查。
例如,如果您在集合中有一个现有文档,如下所示:
{
"_id": 1,
"a": {
"b": {
"crs": [
{
"cr": {
"d": 2,
"e": "two"
}
}
]
}
}
}
如果您随后应用如下验证器:
// Validator for nested array of objects.
var schema = {
"type": "object",
"properties": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"crs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"cr": {
"type": "object",
"properties": {
"d": {
"bsonType": "int",
},
"e": {
"type": "string",
}
}
}}
}
}}
}}
}}}
db.runCommand({"collMod": "collectionName",
"validator": { "$jsonSchema": schema}}
);
当您更新现有文档以添加新的 cr
时,将验证 整个 文档。
db.collectionName.update({_id: 1}, {"$push":{
"a.b.crs":{
"cr":{
"d": NumberInt(20),
"e": "new element"}}}
});
任何根据您的文档架构验证规则无效的现有文档,您必须先更正文档,然后再更新它们。
我有一个 JSON 文件,我想使用 MongoDB 上的 JSON 架构验证该文件。 我如何将 JSON 架构导入 MongoDB,然后验证 JSON 文件。 JSON 文件已经在一个集合中,所以我想在不导入新的 JSON 文件的情况下进行验证。
JSON:
{
"Book": {
"Year:2016-2017": {
"Crs": [{
"Cr": {
"_id": {
"$oid": "5a439ff4fc0900f06fb470a4"
},
"Number": 35,
"Pag": 8,
"Desc": "Embl",
"Ad": "S",
"Type": "Embl"
}
}]
}
}
}
JSON 架构:
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required": false,
"properties": {"Book": {
"type": "object",
"id": "http://jsonschema.net/Book",
"required": false,
"properties": {"Year:2016-2017": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017",
"required": false,
"properties": {"Crs": {
"type": "array",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs",
"required": false,
"items": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0",
"required": false,
"properties": {"Cr": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr",
"required": false,
"properties": {
"Ad": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Ad",
"required": false
},
"Desc": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Desc",
"required": false
},
"Number": {
"type": "number",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Number",
"required": false
},
"Pag": {
"type": "number",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Pag",
"required": false
},
"Type": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Type",
"required": false
},
"_id": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/_id",
"required": false,
"properties": {"$oid": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/_id/$oid",
"required": false
}}
}
}
}}
}
}}
}}
}}}
我想使用 mongodb 的模式验证 JSON。
JSON file is already in one collection so i want to validate without importing a new JSON file.
从 MongoDB v3.6.x 开始,document schema validation 仅在更新和插入期间发生,现有文档在修改之前不会进行验证检查。这意味着您在 MongoDB 集合中的现有 JSON 文档将不会通过您刚刚应用的验证规则进行验证。
请注意 $jsonSchema operator and JSON-SCHEMA 支持是 MongoDB 版本 3.6 中的新增功能。
i want to add a new "Cr" but it needs to respect the JSON Schema.(Insertion/Update)
在集合上指定文档架构验证后,任何更新操作都会触发对受影响文档的验证检查。
例如,如果您在集合中有一个现有文档,如下所示:
{
"_id": 1,
"a": {
"b": {
"crs": [
{
"cr": {
"d": 2,
"e": "two"
}
}
]
}
}
}
如果您随后应用如下验证器:
// Validator for nested array of objects.
var schema = {
"type": "object",
"properties": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"crs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"cr": {
"type": "object",
"properties": {
"d": {
"bsonType": "int",
},
"e": {
"type": "string",
}
}
}}
}
}}
}}
}}}
db.runCommand({"collMod": "collectionName",
"validator": { "$jsonSchema": schema}}
);
当您更新现有文档以添加新的 cr
时,将验证 整个 文档。
db.collectionName.update({_id: 1}, {"$push":{
"a.b.crs":{
"cr":{
"d": NumberInt(20),
"e": "new element"}}}
});
任何根据您的文档架构验证规则无效的现有文档,您必须先更正文档,然后再更新它们。