Open In App

Specify JSON Schema Validation in MongoDB

Last Updated : 29 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB is a NoSQL database which gives us the flexibility to make changes to the Schema when required. There are certain cases where the user might require Validation of the data that is being stored in the Database collection. In this article, we will learn how to add and Specify the JSON Schema Validation while Creating or Updating a MongoDB Collection.

We will be using Mongosh in the below example to understand and add the JSON Schema validation to a MongoDB collection.

Specifying JSON Schema Validation

We can add a JSON Schema Validation to the Collections present in MongoDB. Let’s see how we can do this in the steps below:

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.

Output:

AddSchema

Explanation: In the above image, we can see that the JSON schema validation rules are applied to the collection.

Step 3: Adding Documents to the Collection

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

Output:

InvalidData

Explanation: In the above image, we can see that the document does not get added to 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.

Output:

ValidData

Explanation: In the above image, we can see that the document gets added successfully to the collection.

Conclusion

In conclusion, we learned about the JSON Schema validation in MongoDB, and how we can specify a particular JSON to be applied as a Schema Validation for any MongoDB Schema. Schema validation acts as a filter layer for any documents that get inserted into the collection. We can specify the JSON Schema inside the db.createCollection() command, and pass it as a separate argument. The validation can be applied on fields inside the collection, and the documents that don’t match the validation will not get inserted into the collection.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads