Implementing the Bulk Operation in Mongodb Through NodeJs
Posted By : RAJAT KATIYAR | 26-Dec-2016
The Bulk Operation in mongodb executes the queries in one go, instead of running query one by one. In a normal operation the thread is occupied not allowing the other processes to execute so the bulk operation fires all the queries instantly to mongodb and then mongodb runs those queries sent in bulk mode so that the program in nodejs does not waits for each and every query’s response.
So for implementing the Bulk Operation follow the procedure,
-
Create the db connection object.
-
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://admin:admin@localhost:27017/Weone", function(err, db) {
var Users = db.collection('users');
var Users_Batch = Users.initializeOrderedBulkOp();
for(var i=0;i<100;i++){
var objInsert={};
Users_Batch.insert(objInsert);
}
Users_Batch.execute(function(err,res){
if(!err){
console.log("Success")});
db.close();
}else{
console.log(‘Error’)
db.close();
}
})
More From Oodles
Ready to innovate? Let's get in touch
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
RAJAT KATIYAR
Rajat Katiyar is a very bright web app developer, he has good knowledge of JAVA, C/C++, C#.NET. Apart from this he has interest in theater and sports.