MongoDB Schema Validation makes it possible to easily enforce a schema against your MongoDB database, while maintaining a high degree of flexibility, giving you the best of both worlds. In the past, the only way to enforce a schema against a MongoDB collection was to do it at the application level using an ODM like Mongoose , but that posed significant challenges for developers.
Mongoose is a Node.js based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as SQLAlchemy for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer. In addition to enforcing a schema, Mongoose also offers a variety of hooks, model validation, and other features aimed at making it easier to work with MongoDB.
In this article, we’ll take a look at MongoDB Schema Validation , compare and contrast it to using an ODM like Mongoose, and see how it can help us enforce a database schema, while still allowing for great flexibility when needed, and finally see if the additional features that Mongoose provides are worth the overhead.