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:-
"
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 "
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
- 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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Palak Sharma
Palak is enthusiastic and passionate about coding , Her areas of expertise in coding languages are JavaScript and angular , html and css .