我正在做一個小任務,需要讀取節點中的大文件(即1.3GB)並將每行分隔爲一條記錄,每條記錄都是在更短的時間內插入到mongodb集合中。請在代碼中提示我並提前致謝。在較短的時間內使用nodejs在mongodb中插入最大記錄數
0
A
回答
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小時) –
相關問題
- 1. MongoDB + Nodejs插入最大長度
- 2. Traceview最大記錄時間?
- 3. MongoDB的記錄插入
- 4. 是否有MongoDB的最大記錄數?
- 5. 在MySQL數據庫中插入大量記錄消耗內存
- 6. 用Nodejs在Mongodb中插入大量條目
- 7. 按插入時間在Apache Jena中插入和搜索記錄
- 8. 比較時,並顯示在時間格式最短的時間
- 9. 嘗試在MongoDB中插入記錄時重複鍵_id
- 10. MongoDB中插入記錄的問題
- 11. 在表格中間插入新記錄
- 12. 使用nodejs短時間寫入大量數據無法正常工作
- 13. Ecto插入中間記錄
- 14. 如何在SQL中的特定記錄之間插入記錄
- 15. 嵌套/嵌入模式的多個記錄沒有插入到mongodb中,nodejs
- 16. 使用PHP在MongoDB中插入數據
- 17. NodeJS:在dynamodb中插入記錄(如果不存在)
- 18. 如何使用jquery在最短時間內修改大表的行?
- 19. Haskell Mongodb從文件中插入記錄
- 20. 在很短的時間內多次插入Mysql
- 21. nodejs - MongoDB在插入次數太多時崩潰
- 22. 如何在較短的時間內搜索數據庫
- 23. 在短時間內存儲大量數據時的主鍵
- 24. 在mysql中選擇最大記錄數
- 25. 按mongodb nodejs中的最大值排序
- 26. MongoDB - 在插入記錄時轉義引號
- 27. 插入記錄的最佳數據庫
- 28. 在Postgres中使用MAX函數或DESC的最大記錄
- 29. 如何在codeigniter活動記錄中使用select插入記錄
- 30. 在asp.net中插入記錄
更少的時間那又如何? –
用於循環或任何遞歸方法 – chirag
Jimmy Speckerman學校的另一個畢業生編碼 –