javascript - 多文档事务不使用 MongoDB Atlas
问题描述
更新
经过一些建议,我修改了这样的代码:
const session = await mongoose.startSession()
session.startTransaction()
try {
const udpated = await Schema1.findByIdAndUpdate(
'id', { $set: { /* ... */ } }, { session }
)
const array = await Promise.all(
updated.array.map(async item => {
// change 1
const doc = await Schema2.findById(item.someId).session(session)
const payload = { /* ... */ }
// change 2
return new Schema3(payload).save({ session })
})
)
await session.commitTransaction()
session.endSession()
} catch (err) {
await session.abortTransaction()
session.endSession()
throw err
}
但这给了我另一个错误:
{
MongoError: internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.116:52242->192.168.254.116:27000: i/o timeout
at /some-path/node_modules/mongodb-core/lib/connection/pool.js:581:63
at authenticateStragglers (/some-path/node_modules/mongodb-core/lib/connection/pool.js:504:16)
at Connection.messageHandler (/some-path/node_modules/mongodb-core/lib/connection/pool.js:540:5)
at emitMessageHandler (/some-path/node_modules/mongodb-core/lib/connection/connection.js:310:10)
at TLSSocket.<anonymous> (/some-path/node_modules/mongodb-core/lib/connection/connection.js:453:17)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at TLSSocket.Readable.push (_stream_readable.js:208:10)
at TLSWrap.onread (net.js:597:20)
ok: 0,
errmsg: 'internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.116:52242->192.168.254.116:27000: i/o timeout',
code: 8000,
codeName: 'AtlasError',
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
× Unexpected error occured MongoError: internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.116:52242->192.168.254.116:27000: i/o timeout
at /some-path/node_modules/mongodb-core/lib/connection/pool.js:581:63
at authenticateStragglers (/some-path/node_modules/mongodb-core/lib/connection/pool.js:504:16)
at Connection.messageHandler (/some-path/node_modules/mongodb-core/lib/connection/pool.js:540:5)
at emitMessageHandler (/some-path/node_modules/mongodb-core/lib/connection/connection.js:310:10)
at TLSSocket.<anonymous> (/some-path/node_modules/mongodb-core/lib/connection/connection.js:453:17)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at TLSSocket.Readable.push (_stream_readable.js:208:10)
at TLSWrap.onread (net.js:597:20)
顺便说一句:我还重构了该代码而不使用mongoose
(我只是使用标准mongodb
客户端nodejs
,但我仍然收到这些错误。
由于这个mongoose
问题中提到了我的问题,我正在使用事务。
但是,我的问题是,我的实现Promise.all()
似乎不适用于mongoose
事务。问题可能来自于使用多个Schemas
与一个session
或创建一组文档。(但我真的不确定)
const session = await mongoose.startSession()
session.startTransaction()
try {
const udpated = await Schema1.findByIdAndUpdate(
'id', { $set: { /* ... */ } }, { session }
)
const array = await Promise.all(
updated.array.map(async item => {
const doc = await Schema2.findById(item.someId)
const payload = { /* ... */ }
return Schema3.createa(payload, { session })
})
)
await session.commitTransaction()
session.endSession()
} catch (err) {
await session.abortTransaction()
session.endSession()
throw err
}
我收到错误,Schema3
某些必需路径的验证失败。即使payload
在 console.log 时找到它。
{ ValidationError: xxx validation failed: xxx: Path `xxx` is required., xxx: Path `xxx` is required., xxx: Path `xxx` is required.
at ValidationError.inspect (/xxx/node_modules/mongoose/lib/error/validation.js:59:24)
at formatValue (util.js:400:38)
at inspect (util.js:294:10)
at format (util.js:223:18)
at Console.log (console.js:130:21)
at module.exports (xxx.js:228:17)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
errors:
{ xxx:
{ ValidatorError: Path `xxx` is required.
at new ValidatorError (/xxx/node_modules/mongoose/lib/error/validator.js:29:11)
at validate (/xxx/node_modules/mongoose/lib/schematype.js:871:13)
at /xxx/node_modules/mongoose/lib/schematype.js:924:11
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (/xxx/node_modules/mongoose/lib/schematype.js:880:19)
at /xxx/node_modules/mongoose/lib/document.js:1913:9
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
message: 'Path `xxx` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'xxx',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true },
xxx:
{ ValidatorError: Path `xxx` is required.
at new ValidatorError (/xxx/node_modules/mongoose/lib/error/validator.js:29:11)
at validate (/xxx/node_modules/mongoose/lib/schematype.js:871:13)
at /xxx/node_modules/mongoose/lib/schematype.js:924:11
at Array.forEach (<anonymous>)
at ObjectId.SchemaType.doValidate (/xxx/node_modules/mongoose/lib/schematype.js:880:19)
at /xxx/node_modules/mongoose/lib/document.js:1913:9
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
message: 'Path `xxx` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'xxx',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true },
xxx:
{ ValidatorError: Path `xxx` is required.
at new ValidatorError (/xxx/node_modules/mongoose/lib/error/validator.js:29:11)
at validate (/xxx/node_modules/mongoose/lib/schematype.js:871:13)
at /xxx/node_modules/mongoose/lib/schematype.js:924:11
at Array.forEach (<anonymous>)
at ObjectId.SchemaType.doValidate (/xxx/node_modules/mongoose/lib/schematype.js:880:19)
at /xxx/node_modules/mongoose/lib/document.js:1913:9
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
message: 'Path `xxx` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'xxx',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true } },
_message: 'xxx validation failed',
name: 'ValidationError' }
在不使用mongoose
事务的情况下重构代码时,一切正常:
try {
const udpated = await Schema1.findByIdAndUpdate(
'id', { $set: { /* ... */ } }
)
const array = await Promise.all(
updated.array.map(async item => {
const doc = await Schema2.findById(item.someId)
const payload = { /* ... */ }
return Schema3.createa(payload)
})
)
} catch (err) {
throw err
}
解决方案
我联系了 MongoDB 支持,结果发现这是一个已知问题:
我们目前知道 M0 免费套餐集群存在一个问题,即多语句事务超时并出现错误。这应该随着 MongoDB 版本 4.0.5 的推出而得到解决。同时,如果您迫切需要此功能,我建议您将集群升级到 M10+ 集群。
所以出现问题是因为我使用的是免费套餐。但这个错误有望在 MongoDB 4.0.5 版本中得到修复。
更新
由于我的数据库现在在 4.0.5 版上运行,因此问题已得到解决。所以这不一定是代码的问题。
推荐阅读
- forms - COM 加载项 - 功能区上的大多数弹出按钮无法与全部替换表单区域一起使用
- odoo-14 - odoo 14 错误:连接到数据库失败
- python - 将 Django 模型中的数据与另一个模型相关联
- regex - 如何在 Emacs 中杀死具有模式的每一行:#+NAME:,?
- arrays - 使用 JSON.parse 解析字符串数组到数组?
- firebase - Firestore 中 setDoc 的 getDocFromServer 等价物是什么?确认文档保存在服务器上而不是缓存
- mysql - 为什么等待在我的 mysql connection.query 上不起作用?
- sql - Postgres:UPDATE 并返回 SELECT 语句的结果
- javascript - 对外部 API 的相同请求适用于 go、postman、curl,但不适用于 node.js
- tensorflow - TF模型动物园中的COCO mAP解释