Using an ORM – Mongoose
We can use MongoDB directly, but it will require a bigger understanding and more code to interact with the database. As the objective of this book is to learn Node.js, we will use an ORM to interact with MongoDB. An ORM is a library that allows us to interact with a database using objects instead of SQL queries. In this section, we will use Mongoose (https://mongoosejs.com/). Alternatively, you can use MongoDB Node.js Driver, which is the official MongoDB driver (https://docs.mongodb.com/drivers/node/) for Node.js. The official documentation can be found at https://mongoosejs.com/docs/guide.html.
Mongoose offers several features that are quite convenient for a web application:
- Schema validation: We can define the schema of the documents, and Mongoose will validate the data before saving it to the database
- Model: We can define a model for each collection, and we can use it to interact with the database
- Middleware: We can define middleware...