Open In App

Schema Validation in MongoDB

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

MongoDB is a NoSQL and document-oriented database. MongoDB stores the records in a document in BSON format. Unlike SQL databases, it allows the documents in the collection to have different structures. There are some scenarios where the user needs to have a specific structure for the data stored in the document. This is a common practice to maintain data integrity and consistency.

Most relational databases store data in tabular format, which makes it difficult to store and handle big data. These days data is the key for any application to build. The more data the better the application’s performance. So to handle such big data we need a more reliable and user-friendly database.

A simple document structure in MongoDB will look like this:

{
title: 'GeeksforGeeks',
by: 'SaiRam Padala',
url: 'https://www.geeksforgeeks.org',
type: 'NoSQL'
}


Schema Validation:

Schema validation in MongoDB is a feature that allows us to set the structure for the data in the document of a collection. We follow some set of rules, and validation rules, which ensure that the data we insert or update follows a specific predefined schema and ensures the data must have only specific datatypes, required fields, and validation expressions mentioned in the predefined schema.

When we create a collection for the first time and we want it to meet specific criteria then we can define the collection with the schema validation rules. These validation rules can include specifying the required fields we want, and the datatype for those fields, and also allow the user’s custom expressions. We use the command $jsonSchema for specifying the rules.

When to use Schema Validation?

Schema Validation is like setting rules for how your document must look in your database. When you are experimenting on a new application in which you think that the incoming data might change the application’s fields and you are unsure about the structure you might not want to use schema validation. However, when you understand your application thoroughly and its fields like what type of data each field takes and how much size it can be etc.

In a scenario, you want to have a collection where you store the usernames of employees as a string. In this case, you can specify validation rules for that field which prevents employees from storing usernames with other datatypes.

For instance, you have a collection ‘students’ and you want to have fields like name, id, age,and department for your collection to store in it.

  • Step 1: We create a collection named of ‘students‘ using the createCollection() command.
  • Step 2: With the ‘$jsonShema‘ command inside the validator we specify the schema validation rules. Here with the required property we give a list of fields that every document must have when inserted into the collection.
  • Step 3: Give all the fields and their datatypes inside the properties.
// Example schema validation for a collection named 'Students'
db.createCollection("Students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "id", "age", "department"],
properties: {
name: {
bsonType: "string",
description: "Name must be a string."
},
id: {
bsonType: "int",
description: "id must be an integer."
},
age: {
bsonType: "int",
minimum: 10,
description: "Age must be an integer greater than or equal to 10."
},
dept: {
bsonType: "string",
description: "Department must be a string."
}
}
}
}
});


  • Step 4: Now try to insert the documents one by one into the collection with insertOne() command.

Query:

When we insert data according to validation rules it means Valid Document.

//inserting a record into Students
db.Students.insertOne({
name: "el",
id: 2001,
age: 11,
department: "IS",
});


Output:

valid_document_gfg

Document Inserted Successfully.

When we will try to add Invalid Documents to our collection.

Query:

//inserting a record into Students
db.Students.insertOne({
name: "will",
id: 2002,
age: 14,
});



Output:

invalid_document_gfg

Document failed to insert due to validation.

Explanation: Here, you can see the first document consists of all the required fields in the collection so it is inserted successfully. Whereas, the second document is missing the department field, in this case it is considered an invalid document and throws an error as it doesn’t meet the criteria specified in the schema validation. Hence MongoDB rejects the document to be inserted into the collection.

When does MongoDB Check for Validation?

You should know that whenever you create a new collection with schema validation, MongoDB checks for validations only during updates and inserts in that collection. And when you specify validation rules for a pre-existing, non-empty collection, only the documents that are inserted after are checked for validations. The documents already present in the collection are not checked for validation until they are altered.

When you want to update the already existing schema we use ‘collMod’ command.

db.runCommand({
collMod: "Students",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "id", "age", "department", "marks"],
properties: {
name: {
bsonType: "string",
description: "Name must be a string.",
},
id: {
bsonType: "int",
description: "ID must be an integer.",
},
age: {
bsonType: "int",
minimum: 10,
description: "Age must be an integers greater than or equal to 10.",
},
department: {
bsonType: "string",
description: "Department must be a string.",
},
marks: {
bsonType: "int",
minimum: 0
description: "Marks must be integer greater than or equal to 0.",
},
},
},
},
});



Explanation: Here we included the marks field in the required property. We used ‘collMod’ to modify the schema defined earlier.

Note: ‘collMod’ will fail if the collection has the already existing documents that don’t follow the validation rules. So to modify the schema with ‘collMod’, ensure that the documents already present in the collection must be updated according to the schema or removed.

Conclusion

In conclusion, Schema validation is a set of rules in MongoDB that allows you to specify the criteria and structure for the documents in a collection of ensuring the data to follow a predefined schema with the ultimate aim to ensure data consistency and integrity. This feature helps developers define data standards and promote a well-defined data model within MongoDB databases. However, this schema validation will be a restriction if your application is in the early stages and when you are unsure about your application’s behavior.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads