【发布时间】:2019-10-01 13:48:02
【问题描述】:
我正在尝试在 mongodb 中插入 500 000 条记录 收藏。这些值存储在 csv 中并进行解析,然后存储到数组中。并使用递归函数一一插入记录,当再次插入一条记录时,调用相同的函数。 此过程适用于 200 000 条记录,但当记录大小增加超过 200 000 条时,它会导致堆内存不足(JS 堆栈跟踪)。
下面是我正在使用的递归函数
function insertMongoSingle(fileRows, x, total){
if(x < total){
let item = fileRows.shift();
let record = new Record({i:item}, false);
record.save(function(error, contact){
if(error){
console.log(error);
x++;
insertMongoSingle(fileRows, x ,total);
}else{
x++;
insertMongoSingle(fileRows, x, total);
}
});
}else{
console.log('completed');
}
}
其中 x 是计数,fileRows 是对象数组中的总记录,total 是 fileRows 的长度
【问题讨论】:
-
总只是 fileRows.length?你能展示产生fileRows的函数吗,在1个数组中有500,000个项目效率很低,而且会有更好的方法。
-
不能分块读取数据并处理吗?
标签: javascript node.js mongodb express mongoose