Implementing the Bulk Operation in Mongodb Through NodeJs

Posted By : RAJAT KATIYAR | 26-Dec-2016

Implementing Bulk Operation Through MongoDB in Nodejs

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,

  1. Create the db connection object.

  2. 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();

}

 

})

 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..