簡體   English   中英

如何在Mongo中使用Json模式驗證Json文件?

[英]How do i validate a Json file using Json schema in Mongo?

我有一個JSON文件,我想使用MongoDB上的JSON Schema驗證文件。 如何將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文件已經在一個集合中,因此我想進行驗證而不導入新的JSON文件。

從MongoDB v3.6.x開始, 文檔架構驗證僅在更新和插入期間進行,現有文檔直到進行修改才進行驗證檢查。 這意味着MongoDB集合中的現有JSON文檔將不會通過您剛剛應用的驗證規則進行驗證。

請注意,MongoDB 3.6版中新增了$ jsonSchema運算符JSON-SCHEMA支持。

我想添加一個新的“ Cr”,但它需要遵守JSON模式。(插入/更新)

在集合上指定文檔架構驗證后,任何更新操作都會觸發對受影響文檔的驗證檢查。

例如,如果您在一個集合中有一個現有文檔,如下所示:

{
  "_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"}}}
});

根據您的文檔架構驗證規則,任何無效的現有文檔,您都必須先更正文檔,然后再進行更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM