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.

View Existing Validation Rules in MongoDB

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.

Similar Reads

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

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

Contact Us