Setup Schemas and Models for Users and Course Collection
Understand how to define Mongoose schemas for users and courses, implement password encryption with bcryptjs, and use unique validation to prevent duplicates. Learn to configure and register models for efficient data handling in a MongoDB backend using the MEAN stack.
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
-
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:
Creating the user schema
To create the user schema, we’ll need to create a new file called user.js inside our model folder. ...