【发布时间】:2021-12-23 23:50:55
【问题描述】:
我正在为 bulkWrite 操作而苦苦挣扎。我正在处理 100,000 个数据。以下代码适用于 40k 数据。如果我尝试保存超过 50k 的数据,我会遇到内存问题。
我尝试了 insertMany(),20k 数据失败。 upsert 100,000 个数据的有效方法是什么?
代码:
async bulkInsertData(array) {
const options = [];
await sleep(60000);
for (const item of array) {
options.push({
updateOne: {
filter: { "id": item.id },
update: {
"$set":
{
caseType : item.caseType,
category : item.category
}
},
upsert: true
}
})
};
await sleep(60000);
const result = await this.dataModel.bulkWrite(options , { ordered: false })
console.log(result)
}
错误
<--- Last few GCs --->
[22:0x5f968d0] 1896003 ms: Mark-sweep 502.2 (519.2) -> 498.4 (518.9) MB, 435.5 / 0.0 ms (average mu = 0.158, current mu = 0.051) allocation failure scavenge might not succeed
[22:0x5f968d0] 1896424 ms: Mark-sweep 502.4 (518.9) -> 498.7 (519.9) MB, 392.6 / 0.0 ms (average mu = 0.117, current mu = 0.067) allocation failure scavenge might not succeed
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
【问题讨论】:
-
尝试更新nodejs版本。
-
我使用的是 Nest 8.0.0。此错误发生在使用 2x dyno 运行的 Heroku 中。我正在寻找改进代码的可能方法
-
但是为什么要使用
bulkWrite?我之前遇到过类似的问题,并通过使用 for 循环逐行插入来解决它
标签: node.js mongodb nestjs bulkinsert