View Existing Validation Rules in MongoDB
Last Updated :
28 Dec, 2023
MongoDB is a NoSQL database. MongoDB database can handle large, complex, and unstructured data with easy scalability. It allows us to make changes to the Schema as and when required. The Schema validation rules are a set of rules that are applied to the document when it is added to the collection. If the document adheres to the rules, then it is added, else the MongoDB throws a validation error that prohibits adding the document to the collection. MongoDB also allows us to view the existing schema validation rules that are applied to any collection.
We will be using Mongoose in the below example to understand and view the JSON Schema validation in a MongoDB collection. mongosh is a CLI provided by the MongoDB community itself to interact with its utilities and collections, i.e., without any GUI.
JSON Schema Validation Rules
JSON Schema validation rules are a bunch of rules in a JSON format, that define which document can be added into a collection. The Schema can have rules, like the `age` property should have a minimum value of 18, and so the document, that we are trying to add, will fail to get inserted into the collection if it does not follow the said criteria in the Schema validation.
Viewing Existing Schema Validation Rules
We can view the existing JSON Schema Validation in the collections present in MongoDB. Let's see how we can do this in the steps below -
Step 1: Create a MongoDB Collection and Add the Schema Validation Rules
We will create a users collection and add a JSON Schema Validation rule to the collection. We will be using the below JSON schema as a validator to add to the collection. This JSON Schema contains properties that specify which properties will the documents in the collection have, and the validators on the properties are used for data validation before entering any document in the collection. If the data doesn't pass the validation, MongoDB throws an exception error.
JSON Schema for Validation:
We will create a users collection and add a JSON Schema Validation rule to the collection.
db.createCollection('users', {
validator: {
"$jsonSchema": {
"bsonType": "object",
"properties": {
"first_name": {
"bsonType": "string",
"description": "must be a string and is required"
},
"last_name": {
"bsonType": "string",
"description": "must be a string and is required"
},
"email": {
"bsonType": "string",
"pattern": "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$",
"description": "must be a valid email address"
}
}
}
}
})
Output:
{ ok: 1 }
Step 2: Using the db.getCollectionInfos() Method to View the Validation Rules
We will use the db.getCollectionInfos() method to return the validation rules for the users collection. The db.getCollectionInfos() method provides the meta information related to the collection, like what are the properties, their validations, their types, etc.
Output:

Step 3: Using the listCollections Syntax to Return the Validation Rules for the Collection
We will use the listCollections syntax here to view the validation rules. The listCollections syntax provides us a cursor that contains information about the collection, like the properties and their types, their validations, etc. Both, the listCollections and getCollectionsInfo return the same information, but the output format differs slightly for each command.
Output:

Conclusion
In conclusion, we learned about the JSON Schema validation in MongoDB, and how we can view the applied Schema validations on a given MongoDB collection. There are currently 2 different ways to view the schema validations that we learnt, one is using the db.getCollectionInfos() method, which returns the validation rules for the Users collection, and the other is listCollections that provides a cursor that contains information about the collection.
Similar Reads
Schema Validation in MongoDB
MongoDB schema validation helps keep your data organized and correct. With MongoDB validation rules, we can set up guidelines for what data can be stored in our database. This makes it easier to ensure that all the information we save is reliable and useful. In this article, We will learn about the
6 min read
Modify Schema Validation in MongoDB
MongoDB Validation rules are essential for defining the structure and ensuring the integrity of documents within a collection. These rules determine what constitutes valid and invalid documents and can be modified as data requirements evolve. In this article, We will learn about the Modify Schema Va
6 min read
Bypass Schema Validation in MongoDB
Schema Validation in MongoDB allows defining a structure for documents within a collection, ensuring data integrity and consistency. This validation ensures that documents adhere to specified rules upon insertion and updates. In this article, We will learn about the Bypass Schema Validation in Mongo
5 min read
Create Relationship in MongoDB
In MongoDB, managing relationships between data is crucial for structuring and querying databases effectively. Relationships can be handled using embedded documents, references and the $lookup aggregation stage, each offering different advantages depending on the use case. In this article, We will l
7 min read
Specify JSON Schema Validation in MongoDB
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 specifyi
5 min read
Specify Validation With Query Operators in MongoDB
Validation rules in MongoDB define the structure and content requirements for collections by ensuring data integrity by allowing only valid documents to be inserted. By using query operators, MongoDB validators enforce rules such as field types, required fields and allowable values and thereby preve
5 min read
Mongoose Validation
Mongoose is an elegant solution for modeling and managing MongoDB data in a Node.js environment. One of its powerful features is data validation which ensures that data is correctly formatted and meets specific business rules before being saved to the database. This article explores Mongoose validat
6 min read
Grouping Search Results Using Facets in MongoDB
Faceted search in MongoDB organizes data based on multiple attributes, like categories or tags, using the aggregation framework. This technique enhances data navigation and analysis. To implement faceted search, access MongoDB Cloud, have basic MongoDB query knowledge, and use MongoDB Compass for vi
4 min read
Using Transactions in MongoDB
MongoDB is originally designed as a NoSQL database which has evolved significantly to support more complex operations, including transactions. Since version 4.0, MongoDB supports multi-document transactions and allows developers to perform operations across multiple documents and even multiple colle
7 min read
How to Create and Validate JSON Schema in MongoDB?
JSON Schema validation in MongoDB allows you to enforce the structure of documents in a collection. This ensures data integrity by validating documents against defined schemas before they are inserted or updated. In this article, we will cover how to create and validate JSON Schema in MongoDB using
5 min read