2016-07-30 35 views

回答

0

您可能想要讀取這樣大小的數據而不將其緩衝到內存中。

假設你正在處理JSON數據,我認爲這可能是一個可行的辦法:

var LineByLineReader = require('line-by-line'); 
var fileHandler = new LineByLineReader('path/to/file', { encoding:'utf8', skipEmptyLines: true }); 
var entries = []; 
var bulkSize = 100000; // tweak as needed 

fileHandler.on('error', function (err) { 
    // process errors here 
}); 

fileHandler.on('line', function (line) { 
    entries.push(JSON.parse(line)); 
    if (entries.length === bulkSize) { 
    // pause handler and write data 
    fileHandler.pause(); 

    YourCollection.insertMany(entries) 
    .then(() => { 
     entries = []; 
     fileHandler.resume(); 
    }) 
    } 
}); 

fileHandler.on('end', function() { 
    YourCollection.insertMany(entries) 
    .then(() => { 
    // everything's done, do your stuff here 
    }); 
}); 

line-by-line模塊似乎是一個bit buggy and could be deprecated in the future,所以你可能需要使用linebyline代替

+0

謝謝你的@ Rrance @Francesco ..但我需要它沒有使用緩衝區,並通過使用逐行它花了很多時間插入那些許多記錄(即,4個月記錄/ 1小時) –