No comments yet

Perform CRUD operation for Node Application with MongoDB using Mongoose – Part- 2

[Social9_Share type="horizontal"]

Perform CRUD operation for Node Application with MongoDB

This is Part-2 of the series – Perform CRUD operation for Node Application with MongoDB using Mongoose. In Part-1 we have discussed about installing and connecting to MongoDB , adding Mongoose to our project, creating the Schema and Model. So, the basic setup for performing CRUD operation for Node with MongoDB is done.

Next, lets modify app.js to invoke the employee.js and perform CRUD operation

Invoke Employee

In app.js invoke the Employee model by using require()method.

Create an employee object

Next, create an employee object by giving values for all the properties.

mongo-empobj

This object will be saved as a document in MongoDB.

Create an Employee Document

The method that is used to add a document is save. The syntax is,

  This returns a promise.
  The callback takes three parameters function(err,instance,numaffected){ }
  err – is the error instance if occurred
 instance – the saved document instance
 numaffected 1 if document is saved to db

So, let’s save our employee instance as a document in MongoDB. The code is given below.

mongo-save

The complete code of app.js to create an employee document.

mongo-appadd

Now, go back to command prompt and run the node application as node app.js. you will get the output as shown below.

mongo-apprun1

If you rerun the application again, change the username atleast as it is unique, and then run it. I have added two entries. Now go to the mongoDB console. The collection will saved with ‘s’ appended to the name you have given. In my case it is 'employees'. You can switch to mongo console, query the MongoDB database and get the output by typing,

Retrieve All Employee Documents

There are many ways to retrieve the documents from the database using MongoDB. you can retrieve all documents, or few documents as per condition or only one document using methods like find(), findById(), findOne().

The method to retrieve all the documents is

The callback takes two parameters function(err, result){ }
The result is displayed in JSON format. In case of errors, the result will be null.

Let’s try retrieving the documents from the database.

mongo-find

Run the application in the console as node app.js and check the output.

Retrieve One Document

To retrieve one document use the method findOne() or findById(). The syntax for findOne() is,


mongo-findone

Run the application in the console as node app.js. you can check in the mongo console by typing db.employees.find().pretty().

Update an Employee Document

You can use queries to update all the documents or one document. To update a document the method used is,

where the first three parameters are objects and the last one is a callback function.
The conditions are like where clause(age greater than or less than),document specifies updated values, options specifies multiple updation, overwriting, upsert(create a doc if not exists) and so on all as boolean values.

Lets update the city of both the employee documents.

mongo-update

Update One Document

To update one document the method is findOneAndUpdate()

The code is given below.

mongo-findandup

Run the application as node app.js in the command prompt. Then go to mongo console and check the output by typing db.employees.find().pretty()

Delete an Employee Document

The method to remove a document is,

Lets remove one employee document with the name as Ram. The code is given below.

mongo-remove

Run the application. Now, this entry is removed . you can check also check in the mongoDB console.

The complete code of app.js is given below.


Thats it. So, we have performed CRUD operation for a Node Application which is connected to MongoDB database using Mongoose.

Happy Learning

Post a comment