Specify JSON Schema Validation in MongoDB
Last Updated :
11 Feb, 2025
MongoDB is a flexible, scalable, and powerful NoSQL database, and one of its notable features is JSON Schema Validation. This feature allows developers to enforce data integrity within MongoDB collections, ensuring that documents inserted into a collection adhere to a specific structure. By specifying a set of validation rules, we can prevent erroneous data and maintain consistency across documents.
In this article, we will explain how to specify JSON Schema Validation in MongoDB and explore its practical implementation, ensuring that our MongoDB collections maintain the required structure and integrity.
What is JSON Schema Validation in MongoDB?
JSON Schema Validation in MongoDB allows us to enforce constraints on documents inserted into collections. These constraints ensure that documents conform to a defined structure, improving data consistency, and preventing invalid entries from being added to our database.
By using $jsonSchema in MongoDB, we can define validation rules for fields in a document. These rules can specify data types, required fields, patterns (like regex for emails), and other conditions that documents must meet.
Step 1: Creating a JSON Schema
In this step, We will Create a JSON object that will specify the validation rules that need to be set on the MongoDB collection. It will specify the validation rules on all the properties of the collection.
First, let's create a JSON schema that we will use as a validation while creating new entities inside the collection. Let's create a Schema that takes in first name, last name, and email, and the email should be valid, and all of them will be required.
The JSON that we will use for the Schema Validation:
{
$jsonSchema: {
bsonType: "object",
properties: {
first_name: {
bsonType: "string",
description: "must be a string",
},
last_name: {
bsonType: "string",
description: "must be a string",
},
age: {
bsonType: "int",
description: "must be an integer",
},
email: {
bsonType: "string",
pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
description: "must be a valid email address",
},
},
}
}
Step 2: Adding the JSON Schema Validation to the Collection
In this Step, We will pass the above created JSON as an argument to the db.createCollection() command, so as to add the JSON Schema validation to the created collection.
Now, let's create a collection with the name, Users, and add the validation to the collection. The collection will contain first_name, last_name, age, and email as fields.

Explanation: In this step, the schema validation is applied to the users collection. The validator key ensures that any document inserted into this collection will be validated against the defined schema.
Step 3: Adding Documents to the Collection
Once the schema is in place, we can begin adding documents to the collection. Let’s explore how MongoDB handles documents that pass or fail schema validation.
Part A: Adding a Document with Invalid Data
In this step, we will try to add a document that does not create any cause against to the JSON validation rules.
Now, if we try to add a document that doesn't meet the validation criteria, then we will get a MongoDB validation error. In the image below, we try to add a user with an invalid email, and so we get a validation error.

Explanation: In the above image, we can see that the document does not get added to the collection. The document fails validation because the email format doesn’t match the specified pattern (it should be a valid email address). MongoDB prevents this invalid data from being inserted into the collection.
Part B: Adding a Document with Valid Data
In this step, we will try to add a document that does follows to the JSON validation rules.
If we try to add a document that follows the required validation criteria, then we are able to add the document successfully to the Collection. In this image below, we insert a new user with the correct email this time, and the user gets successfully added to the users collection.

Explanation: In the above image, we can see that the document gets added successfully to the collection. This document passes the validation rules, as the email is valid, and all required fields are provided.
Benefits of JSON Schema Validation in MongoDB
- Data Integrity: Schema validation helps ensure that only valid data is inserted into your collections. By defining clear rules for document structure, you can avoid issues with malformed data, reducing errors in your application.
- Consistency: By enforcing data types and validation rules, MongoDB helps maintain consistency across documents. This is especially important in applications that rely on specific data formats.
- Prevents Invalid Data: JSON Schema Validation prevents invalid or incomplete data from being inserted into the collection, which is critical for applications that require strict data accuracy.
Conclusion
In conclusion, we learned how to specify JSON schema validation in MongoDB, ensuring that documents inserted into our collections meet predefined structural rules. This feature is essential for maintaining data integrity, consistency, and preventing errors. By using $jsonSchema, MongoDB allows developers to set validation rules for fields, enforce correct data types, and validate formats like email addresses. By adopting JSON schema validation in our MongoDB collections, we ensure that our application maintains high-quality data and operates smoothly, even as the database grows.
Similar Reads
SQL Interview Questions
Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS
In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
8 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
ACID Properties in DBMS
In the world of Database Management Systems (DBMS), transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliabilit
8 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read