如何在MongoDB中使用JSON Schema验证已有集合的JSON数据
MongoDB导入JSON Schema并验证已有集合数据的完整方案
我来一步步帮你搞定MongoDB里用JSON Schema验证已有数据的问题——MongoDB自带的文档验证功能正好能满足你的需求,不管是新插入的还是已存在的数据都能处理。下面是针对你的具体情况的操作步骤:
一、先确认你的JSON Schema适配MongoDB要求
你提供的是JSON Schema Draft 03版本,MongoDB 3.6+对这个版本是兼容的,不过它更推荐使用Draft 4、6、7或2019-09版本。不过没关系,你的现有Schema可以直接拿来用,只要稍微包装成MongoDB能识别的命令格式就行。
二、给目标集合添加JSON Schema验证规则
假设你的集合名叫books(如果不是,替换成你实际的集合名),打开Mongo Shell执行以下命令:
db.runCommand({ collMod: "books", validator: { $jsonSchema: { "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", "required": false }, "Desc": { "type": "string", "required": false }, "Number": { "type": "number", "required": false }, "Pag": { "type": "number", "required": false }, "Type": { "type": "string", "required": false }, "_id": { "type": "object", "required": false, "properties": { "$oid": { "type": "string", "required": false } } } } } } } } } } } } } } }, validationLevel: "strict", validationAction: "error" })
参数说明:
collMod:用于修改集合的属性,这里用来添加验证规则$jsonSchema:告诉MongoDB使用JSON Schema进行验证validationLevel:可选,strict表示对所有读写操作生效,moderate只对修改后的数据生效validationAction:可选,error表示不符合规则就阻止操作并报错,warn只发出警告不阻止操作
如果是新建集合时就添加验证规则,可以用createCollection命令:
db.createCollection("books", { validator: { $jsonSchema: { /* 粘贴上面的Schema内容 */ } }, validationLevel: "strict", validationAction: "error" })
三、验证集合中已有的数据
添加完验证规则后,新插入或更新的数据会自动验证,但已有数据需要手动检查。用以下命令找出所有不符合Schema的文档:
db.books.find({ $nor: [{ $jsonSchema: { "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", "required": false }, "Desc": { "type": "string", "required": false }, "Number": { "type": "number", "required": false }, "Pag": { "type": "number", "required": false }, "Type": { "type": "string", "required": false }, "_id": { "type": "object", "required": false, "properties": { "$oid": { "type": "string", "required": false } } } } } } } } } } } } } } }] })
如果命令返回空结果,说明你的所有已有数据都符合指定的JSON Schema;如果有返回文档,就是不符合规则的数据,你可以针对性修改。
小提示
- 你提供的原始JSON数据里的
"Year:2016-2017"键名包含冒号,这在MongoDB里是合法的,Schema里已经正确处理了,不用额外调整 - MongoDB的JSON Schema支持大部分标准特性,但
id字段只是元数据,不会影响验证逻辑,你可以根据需要保留或删除它
内容的提问来源于stack exchange,提问作者pp fernandes
相关产品推荐
相关产品推荐

