DEV Community

Cover image for Introduction to Mongoose and why should we use it
Abdur Rahman
Abdur Rahman

Posted on

Introduction to Mongoose and why should we use it

In this short blog we’ll try to explain what is mongoose, why should we use it and what is the difference between Mongoose and MongoDB. So, let’s get started.

MongoDB is a No-SQL database management system that uses a document-based data structure with no relation between data. We use “mongodb” package to use the MongoDB database in our projects. Mongoose is like another package that lets us use MongoDB database in our projects too but this time we get some advanced features like Object Data Modeling (ODM) and schema validation.

When using the raw MongoDB package the data were stored as a BSON type document. So, we had to convert it to access it and make it usable. But when using Mongoose, it uses ODM to enable us to use the data as a javascript object which makes it much easier.

Also, it has schema validation which we can implement to validate the data we receive. We can create custom rules to accept only specific data types which makes it much better to validate and store the data.

So, what are the key benefits of using Mongoose? Here they are –

  1. It gives us a package to use ODM and schema validation which makes it much easier to work with the database.

  2. Even though we use a NoSQL or non-relational database system but when actually working with it we need a relation between the data. So, mongoose creates a relation between the data even though it is NoSQL.

  3. It uses ~ 40% - 60% less code compared to using the raw MongoDB package. So, we can do the same amount of work with fewer codes.

Top comments (0)