javascript - 如何防止“MongoError:事务 1 已提交。”?
问题描述
问题
使用 MongoDB 4.2,我正在尝试执行 50,000 个事务(50,000 个数组突变)。就像 90 % 是成功的,但对于其余的我得到:
MongoError:事务 1 已提交。
不幸的是,我什至不明白错误信息!
方法
一位 Reddit 用户建议升级到 4.2 以克服 16 MB oplog 的限制。但这没有帮助。
代码
我认为这无关紧要,但为了完整起见,我的 Node.js 代码:
const rowCollection = client.db('docemur').collection('row')
session.startTransaction()
// Reorder rows according to columns
await rowCollection
.find({ table: table._id }, { session })
.forEach(async row => {
// Reorder the row array
row.row = sortBy(row.row, unsorted => {
return findIndex(columns, sorted => unsorted.column === sorted.identifier)
})
// Update the now ordered row
await rowCollection
.updateOne(
{ _id: row._id },
{ $set: { row: row.row } },
{ session }
)
})
await session.commitTransaction()
session.endSession()
解决方案
更新这么多文档时,您需要使用bulkWrite 。
推荐阅读
- swift - UISearchController setActive 什么都不做
- swift - 忽略安全区域中的安全区域
- python - 如何在另一个窗口中随时通过按键跳出循环?
- reactjs - 如何在 MUI 5 中使用美元符号 ($) 覆盖样式?
- r - 带排列国家多边形的图表
- python - 为什么 Telethon cleint.get_entity(id) 打印错误“缺少参数:'实体'”?
- javascript - 为什么服务器表情符号 .author 为空?不和谐的js
- powershell - 我如何“实例”Register-ScheduledTask 并稍后修改它?
- javascript - 在执行自动生成的按键 Python 时抑制用户输入按键
- node.js - TypeORM 不会在由 TypeORM 创建的项目中生成迁移