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.
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.
Comments