Perform CURD Operations Using MongoDB

Posted By : Palak Sharma | 27-Dec-2017

MongoDB is a document database which is free and flexible and can able to handle complex data very easily. There is number of drivers which are created to establish a connection of MongoDB with a node js application. Initially, to start using MongoDB in our application, we need to get the drivers installed and establish a connection. To install the drivers, the command is very simple as stated:- 

"npm install MongoDB"

Then, once it is installed in our system, move to the folder where it is installed and run the command "mongodb.exe". It will initiate our server. 

Secondly, we need to get the reference to the MongoDB client and establish a connection.

var mongoClient=require('mongodb').MongoClient;
var mongoDbObj;

mongoClient.connect('mongodb://localhost/studentDb', function(err, db){
  if(err)
    console.log(err);
  else{
    console.log("Connected to MongoDB");
    mongoDbObj={db: db,
      students: db.collection('students')
    };
}

In above code, we are trying to get the access to "studentDb' database using MongoDB. If a database is not present, it will automatically create a new one with the same name.

mongoDbObj.students.find().toArray(function(err, data){
  if(err){
    console.log(err);
  else{
    //operate with the deta
  }
});

The "find" method returns the object in the form of a document. To convert the data obtained into Javascript Array, we use "toArray" method. Now once our collection is created or say our database is generated, we can perform curd operation like "creating", "deleting", "updating", "retrieving".

The "use" command in MongoDB is used to create the database. For eg:- use studentdb

  • Inserting a  data into a database is quite easy, it only requires an object to be inserted, and a callback to handle success and failure.
mongoDbObj.students.insert(newStudent,{w:1},function(err, result){
  if(err){
    //Handle the failure case
  }
  else{
    //Handle the success case
  }
});?
  • Updating a data, it will replace the data with the matched object.
mongoDbObj.students.update({studentId:1},{$set: {name:”Ravi Kiran”}},{w:1}, function(err, result){
  //Handle success and failure
});
  • Deleting a data, just use remove() method, it will call off the data automatically.
mongoDbObj.students.remove({studentId:studentId},{w:1},function(err, result){
    //Handle success and failure
}); 
  • Retrieving a  database, use find() method, we can pass the specified condition to display back the matched object.
mongoDbObj.students.find({studentId:1})    
mongoDbObj.students.find({studentId:{$gte:2}})    
mongoDbObj.students.find({"marks.totalMarks":500})   
mongoDbObj.students.find({"marks.totalMarks":{$lte:500}}) 

Hence, we can perform various operations using MongoDB and through our application as well.

About Author

Author Image
Palak Sharma

Palak is enthusiastic and passionate about coding , Her areas of expertise in coding languages are JavaScript and angular , html and css .

Request for Proposal

Name is required

Comment is required

Sending message..