Use Schema Validator to apply JSON Schema, the recommended way of performing MongoDB schema validation on a collection. Try it today.
JSON Schema is a standard format for defining the structure of JSON data to enforce consistency and data validity, for example required fields, maximum, or minimum values. You can create schema validation rules using keywords to apply constraints to ensure that all documents in a MongoDB collection contain valid data. A JSON Schema is a JSON document.
Example of JSON Schema
Below is the JSON Schema that we’ll apply to the Customers collection:
{ "bsonType" : "object", "required" : [ "title", "first", "last", "email", "dob", "address.street.name", "address.street.suffix", "address.street.number", "address.city", "address.state", "address.zip-code", "user_name", "package", "prio_support", "registered_on", "transactions", "pet", "number_pets", "Studio_3T_edition", ], "properties" : { "title" : { "bsonType" : "string", "description" : "must be a string and is required" }, "first" : { "bsonType" : "string", "description" : "must be a string and is required" }, "last" : { "bsonType" : "string", "description" : "must be a string and is required" }, "email" : { "bsonType" : "string", "description" : "must be a string and is required" }, "dob" : { "bsonType" : "date", "description" : "must be a date and is required" }, "address.street.name" : { "bsonType" : "string", "description" : "must be a string and is required" }, "address.street.number" : { "bsonType" : "string", "description" : "must be a string and is required" }, "address.street.suffix" : { "bsonType" : "string", "description" : "must be a string and is required" }, "address.city" : { "bsonType" : "string", "description" : "must be a string and is required" }, "address.state" : { "bsonType" : "string", "description" : "must be a string and is required" }, "address.zip_code" : { "bsonType" : "string", "description" : "must be a string and is required" }, "user_name" : { "bsonType" : "string", "description" : "must be a string and is required" }, "package" : { "bsonType" : "string", "description" : "must be a string and is required" }, "prio_support" : { "bsonType" : "string", "description" : "must be a string and is required" }, "registered_on" : { "bsonType" : "date", "description" : "must be a date and is required" }, "transactions" : { "bsonType" : "int32", "description" : "must be an integer and is required" }, "pet" : { "bsonType" : "string", "description" : "must be a string and is required" }, "number_pets" : { "bsonType" : "int32", "description" : "must be an integer and is required" }, "Studio_3T_edition" : { "bsonType" : "string", "description" : "must be a string and is required" } } }
Add MongoDB schema validation
- Right-click the target connection in the Connection Tree.
- Select Add/Edit Validator.
Paste JSON Schema validation rules
In the Edit Validator dialog, type or paste your validation rules using JSON Schema.
JSON Schema is itself a JSON document, so you can validate the JSON that you enter in the text box before saving it to your MongoDB collection.
Set validation levels
You can choose how MongoDB applies schema validation rules using the validation level:
- off – MongoDB disables schema validation
- strict (default setting) – MongoDB applies schema validation rules to all inserts and updates
- moderate – MongoDB applies schema validation rules to inserts and to updates to existing documents that already fulfill the validation criteria
Set validation action
MongoDB can handle documents that don’t meet the schema validation rules in the following ways:
- error (default setting) – Rejects inserts or updates that do not meet the schema validation rules
- warn – Allows inserts and updates while logging violations
An example of an error message that Studio 3T displays when you try to insert a document that doesn’t meet the schema validation rules:
Validate and save validation rules
Click Validate JSON to make sure the JSON is correct in the JSON Schema document, then click Save.
JSON Schema validation tutorial
Want to dive deeper into this topic? Read our tutorial How to Create and Validate JSON Schema in MongoDB Collections to learn more about how you can use JSON Schema validation tools and Schema Explorer to help create JSON Schema.
To learn more about how MongoDB applies schema validation, see the MongoDB documentation.
This article was originally published by Kathryn Vargas and has since been updated.