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

pp fernandes

I have a JSON file and i want to validate the file using JSON Schema on MongoDB. How do i import the JSON Schema to MongoDB and then validate the JSON File. JSON file is already in one collection so i want to validate without importing a new JSON file.

JSON:

{
"Book": {
    "Year:2016-2017": {
        "Crs": [{
            "Cr": {
                "_id": {
                    "$oid": "5a439ff4fc0900f06fb470a4"
                },
                "Number": 35,
                "Pag": 8,
                "Desc": "Embl",
                "Ad": "S",
                "Type": "Embl"
            }
        }]
    }
}

}

JSON schema:

{
"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
                            }}
                        }
                    }
                }}
            }
        }}
    }}
}}}

I want to validate the JSON using the schema with mongodb.

Wan Bachtiar

JSON file is already in one collection so i want to validate without importing a new JSON file.

As of MongoDB v3.6.x, document schema validation only occurs during updates and inserts, existing documents do not undergo validation checks until modification. This means that your existing JSON document in a MongoDB collection will not be validated by the validation rule(s) that you just applied.

Note that $jsonSchema operator and JSON-SCHEMA support is new in MongoDB version 3.6.

i want to add a new "Cr" but it needs to respect the JSON Schema.(Insertion/Update)

Once you specified a document schema validation on a collection, any update operations will trigger validation checks on the affected documents.

For example, if you have an existing document in a collection as below:

{
  "_id": 1,
  "a": {
    "b": {
      "crs": [
        {
          "cr": {
            "d": 2,
            "e": "two"
          }
        }
      ]
    }
  }
}

If you then apply a validator as below:

// 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}}
);    

When you update the existing document to add a new cr, the whole document will be validated.

db.collectionName.update({_id: 1}, {"$push":{
                                       "a.b.crs":{
                                            "cr":{
                                               "d": NumberInt(20),
                                               "e": "new element"}}}
});

Any pre-existing documents that are not valid according to your document schema validation rule(s), you will have to correct the documents first before updating them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I validate JSON using an existing schema file in Rust?

How Do I Validate a JSON file With a Schema in VB.Net?

How do I validate a JSON Schema schema, in Python?

How do I validate an instance of JSON schema against a swagger schema?

How do I validate a JSON response against a Schema in SoapUI

How can I validate JSON in VS Code using a schema file defined in the workspace?

How can I validate the string value of a key using JSON Schema?

How to validate json schema using avj and postman

Can't validate a json with a schema using an external schema file

How to validate JSON schema in java?

How can I validate a JSON string against a schema programmatically?

How can I structure my JSON schema to validate for DynamoDB and RESTAPI?

How to validate for nullable types using json schema validator?

How do i validate a dynamic JSON key?

How do I validate JSON data in a form

How do I validate an array of JSON objects in a json object?

Validate JSON schema compliance with Jackson against an external schema file

How to validate a JSON schema with nested references

Json schema validator, how to validate if key is not static

Validate Json Request Schema

How do I do JSON schema validation using Apigee Edge policies?

Validate JSON from Mongo?

validate array json contains several unordered objects using json schema

How do I update JSON file using PowerShell

How do I parse a json file using gson?

How do I clear a field in json using batch file?

How do I update the data saved in a JSON file using JavaScript?

How do i read a JSON file content using POLLENRICH

How do I pull objects from a JSON file using python?