Mongoose models and schemas

The Mongoose package is an object data modeling library designed to work in an asynchronous environment. It’s also similar to an Object Relational MapperThis is a technique of mapping data from a database using an object-oriented paradigm… (ORM). Mongoose has many advantages in structuring the models and schemas in a database. Some of the benefits include the following:

  • Easy data validation on the fly.

  • It’s asynchronous by default. That is, we can set predefined events before a document gets saved.

  • It provides an easy abstraction of data.

The Mongoose model allows us to access data from MongoDB in an object-oriented pattern. To create a Mongoose model, we need to define a specific schema object. Once that’s done, we’ll need to register the model with Mongoose so we can access the schema anywhere in our application.

The illustration below shows the relationship that exists between the database schema that we’ll create in our application:

Get hands-on with 1200+ tech skills courses.