Skip to content Skip to sidebar Skip to footer

Closing Mongodb Connection In Node.js While Inserting Lot Of Data

I am trying write a program to parse and insert iis logs data in to mongodb. The files aren't that huge it's around 600 lines. Trying to convince my management nodejs and mongodb i

Solution 1:

I'm 100% sure but as far as I can see you are inserting data synchronous. I mean once you get a line you try to insert it and don't wait for the result. Try using another approach:

  • read all the lines and store them in an array
  • start inserting the data line by line asynchronously

Something like that:

var lines = [];
var readAllLines = function(callback) {
    // store every line inside lines array// and call the callback at the endcallback();
}
var storeInDb = function(callback) {
    if(lines.length === 0) {
        callback();
        return;
    }
    var line = lines.shift();
    collection.insert(line, function (err, docs) {
        storeInDb(callback);
    });
}

mongoClient.open(function (err, mongoClient) {
    console.log(err);
    if (mongoClient) {
        readAllLines(function() {
            storeInDb(function() {
                // lines are inserted// close the db connection
            })
        });
    }
});

Post a Comment for "Closing Mongodb Connection In Node.js While Inserting Lot Of Data"