CRUD operations using Mongoose in NodeJS


0

Connection to MongoDB

To establish a connection to MongoDB first we need to import mongoose module to our project which is available in the NPM registry.


** Schema **

Once the connection established we need to define a schema. We use schema to define the shape of a document within a collection in MongoDB. Schema is specific to the mongoose.

Let's see how to design a schema


List of types in Schema

String, Number, Date, Buffer, Boolean, ObjectId, Array

** Model**

Once we create a schema we need to compile that schema into a model. Schema is a class and model is an object to the class.
Let's create a model for the above schema.


** CRUD Operations **

** Create **

Once we create a model then we can create, read, update and delete a document from the database. Let's see how to create a document in the database.


** Read **:

Let's see how to query a document


** Update **:

To update a document


** Delete **:

To remove a document from the database