closing mongodb connection in node.js while inserting lot of data

前端 未结 1 1100
无人共我
无人共我 2021-01-17 03:22

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

相关标签:
1条回答
  • 2021-01-17 04:10

    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 end
        callback();
    }
    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
                })
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题