mongoDB CRUD operation using mongoose and Expressjs
By: Muhammad Zain Tariq
Tags: Mongoosejs, Crud Operations, Mongoose Crud, Mongoose, Mongodb

Step by Step guide for Basic crud operation using mongoose

In this Blog, You will Learn

What is mongoose ?

Create Database and Collection

CRUD operation using mongoose

What is mongoose ?

Mongoose is an object data modeling (ODM) library that provides a rigorous modeling environment for your data, enforcing structure as needed while still maintaining the flexibility that makes MongoDB powerful.

So for Downloading mongoose in your project just run the following command

npm i mongoose

Create Database and Collection

Before creating database and collection you must know that our new database will only be visible on mongoDB compass if we have entered any Document in it. So our Database will be visible in our insert section

Now next step is creating a database using mongoose.

For this first we have to open our mongoDB compass and than click on the link as shown in figure below for connection locally

and now we have to click at connect button as shown below

now we have successfully connected and on both side your current databases as shown in below figure.

Create a Database using mongoose

For using mongoose in your project first we must always require mongoose using following syntax in your root file

So for creating a Database using mongoose we will use following code

here databasename is for your database name and in my case it was practice as shown in figure

Creating Collection using mongoose

So for creating a collection using mongoose first we will specify our schema for the collection

Creating Schema of a Collection

schema defines the structure of the document so for creating our schema named data with a schema for 4 entities (name, degree, age and skill) by writing following code

create a collection

for creating our first collection we will use model

Model: mongoose model is wrapper on the mongoose schema

for creating collection .Use following code for collection creation

in double quotation write your collection name and it must be singular and than move to your project root directory and Run node filename.js in my case it was app.js as shown in figure.

Connection successfully Now we will insert a document in our collection so that database become visible in our mongoDB compass.

CRUD operation using mongoose

Their are 4 operations in crud operation and we will perform it using mongoose so first we will create a database using mongoose.

Create/Insert a document using mongoose

For creating or inserting a document in our mongoose their are two ways of insertion which are

Single insertion Multiple insertion

Single insertion

For single insertion we will be using following code with respect to our schema which we created above

and now run node app.js in your terminal as shown in figure

now to confirm our database is created and document is inserted from mongoDB compass so open its window and than first refresh the mongoDB compass from the Refresh sign as shown in figure.

and after refresh you can saw that our new database and collection is created and document is also inserted as shown in figure below

Multiple insertion

For multiple insertion we will be using following code

Now again run node app.js on your terminal as shown in figure

and refresh your mongoDB compass and you will see that database have more documents as shown in figure

Hurry !we under stand how to do a basic insert operation using mongoose.

Read using mongoose

For reading documents from mongoose we will perform following code for reading our documents from database

and after this run node app.js on your terminal and you will get all of your documents in database as shown in figure below

Update using mongoose

For performing update operation using mongoose we will be using two operation

single document update multiple document update

Single document update

for performing single document update we have two commands one is updateOne and one is findByIdAndUpdate if you are updating by id you must prefer findByIdAndUpdate otherwise updateOne is better but we are updating w.r.t degree so updateOne is used following is code for single update

in above code we update one document with degree BSCS name to Amir so now run node app.js as shown below

and it shows one modification done lets see in our mongoDB compass so refresh it