Multi-Document Transactions not Working using MongoDB Atlas
UPDATE
After some suggestions I modifies the code like this:
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
}
But that gives me another error:
{
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)
Btw.: I also refactored that code without using mongoose
(I just used the standard mongodb
client for nodejs
and I am still getting those errors.
I am using mongoose
transactions because of my problem referred to in this question.
However, my problem is, that my implementation of Promise.all()
doesn't seem to work with mongoose
transactions. The issue probably comes from using multiple Schemas
with one session
or creating an array of documents. (But I am really not sure)
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
}
I am getting errors, that the validation of Schema3
failed for some required paths. Even though payload
is found when console.log it.
{ 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' }
When refactoring the code without using mongoose
transactions, everything works just fine:
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
}
javascript node.js mongodb mongoose mongodb-atlas
|
show 3 more comments
UPDATE
After some suggestions I modifies the code like this:
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
}
But that gives me another error:
{
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)
Btw.: I also refactored that code without using mongoose
(I just used the standard mongodb
client for nodejs
and I am still getting those errors.
I am using mongoose
transactions because of my problem referred to in this question.
However, my problem is, that my implementation of Promise.all()
doesn't seem to work with mongoose
transactions. The issue probably comes from using multiple Schemas
with one session
or creating an array of documents. (But I am really not sure)
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
}
I am getting errors, that the validation of Schema3
failed for some required paths. Even though payload
is found when console.log it.
{ 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' }
When refactoring the code without using mongoose
transactions, everything works just fine:
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
}
javascript node.js mongodb mongoose mongodb-atlas
I believe that if you use Promise.all you don't need async/await inside the map function. Try to remove it.
– Sagi Rika
Jan 2 at 9:51
Did not work :-(
– Florian
Jan 2 at 9:59
is createa an async function? or only find by id?
– Sagi Rika
Jan 2 at 10:00
create
returns aPromise
– Florian
Jan 2 at 10:02
1
I have version 4. I contacted the support and it's a known problem that will be fixed with version 4.0.5. Thank you anyways :)
– Florian
Jan 9 at 8:28
|
show 3 more comments
UPDATE
After some suggestions I modifies the code like this:
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
}
But that gives me another error:
{
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)
Btw.: I also refactored that code without using mongoose
(I just used the standard mongodb
client for nodejs
and I am still getting those errors.
I am using mongoose
transactions because of my problem referred to in this question.
However, my problem is, that my implementation of Promise.all()
doesn't seem to work with mongoose
transactions. The issue probably comes from using multiple Schemas
with one session
or creating an array of documents. (But I am really not sure)
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
}
I am getting errors, that the validation of Schema3
failed for some required paths. Even though payload
is found when console.log it.
{ 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' }
When refactoring the code without using mongoose
transactions, everything works just fine:
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
}
javascript node.js mongodb mongoose mongodb-atlas
UPDATE
After some suggestions I modifies the code like this:
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
}
But that gives me another error:
{
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)
Btw.: I also refactored that code without using mongoose
(I just used the standard mongodb
client for nodejs
and I am still getting those errors.
I am using mongoose
transactions because of my problem referred to in this question.
However, my problem is, that my implementation of Promise.all()
doesn't seem to work with mongoose
transactions. The issue probably comes from using multiple Schemas
with one session
or creating an array of documents. (But I am really not sure)
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
}
I am getting errors, that the validation of Schema3
failed for some required paths. Even though payload
is found when console.log it.
{ 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' }
When refactoring the code without using mongoose
transactions, everything works just fine:
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
}
javascript node.js mongodb mongoose mongodb-atlas
javascript node.js mongodb mongoose mongodb-atlas
edited Jan 6 at 6:15
Florian
asked Jan 2 at 9:17
FlorianFlorian
45423
45423
I believe that if you use Promise.all you don't need async/await inside the map function. Try to remove it.
– Sagi Rika
Jan 2 at 9:51
Did not work :-(
– Florian
Jan 2 at 9:59
is createa an async function? or only find by id?
– Sagi Rika
Jan 2 at 10:00
create
returns aPromise
– Florian
Jan 2 at 10:02
1
I have version 4. I contacted the support and it's a known problem that will be fixed with version 4.0.5. Thank you anyways :)
– Florian
Jan 9 at 8:28
|
show 3 more comments
I believe that if you use Promise.all you don't need async/await inside the map function. Try to remove it.
– Sagi Rika
Jan 2 at 9:51
Did not work :-(
– Florian
Jan 2 at 9:59
is createa an async function? or only find by id?
– Sagi Rika
Jan 2 at 10:00
create
returns aPromise
– Florian
Jan 2 at 10:02
1
I have version 4. I contacted the support and it's a known problem that will be fixed with version 4.0.5. Thank you anyways :)
– Florian
Jan 9 at 8:28
I believe that if you use Promise.all you don't need async/await inside the map function. Try to remove it.
– Sagi Rika
Jan 2 at 9:51
I believe that if you use Promise.all you don't need async/await inside the map function. Try to remove it.
– Sagi Rika
Jan 2 at 9:51
Did not work :-(
– Florian
Jan 2 at 9:59
Did not work :-(
– Florian
Jan 2 at 9:59
is createa an async function? or only find by id?
– Sagi Rika
Jan 2 at 10:00
is createa an async function? or only find by id?
– Sagi Rika
Jan 2 at 10:00
create
returns a Promise
– Florian
Jan 2 at 10:02
create
returns a Promise
– Florian
Jan 2 at 10:02
1
1
I have version 4. I contacted the support and it's a known problem that will be fixed with version 4.0.5. Thank you anyways :)
– Florian
Jan 9 at 8:28
I have version 4. I contacted the support and it's a known problem that will be fixed with version 4.0.5. Thank you anyways :)
– Florian
Jan 9 at 8:28
|
show 3 more comments
3 Answers
3
active
oldest
votes
I contacted the MongoDB Support and it turned out that this is a known issue:
We are currently aware of an issue with the M0 Free Tier clusters whereby multi-statement transactions timeout with an error. This should be fixed with the rollout of MongoDB version 4.0.5. In the meantime, if you require this feature urgently, I would recommend that you upgrade your cluster to an M10+ cluster.
So the issue occurs because I am using the free tier. But the bug will hopefully be fixed with the MongoDB 4.0.5 release.
UPDATE
As my database runs now on version 4.0.5, the problem is fixed. So it wasn't necessarily a issue with the code.
add a comment |
Try adding .session(session)
to each Query
const doc = await Schema2.findById(item.someId).session(session)
https://mongoosejs.com/docs/api.html#query_Query-session
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
add a comment |
Looks like you're missing a session option in findOne()
:
const doc = await Schema2.findById(item.someId, null, { session })
See: https://mongoosejs.com/docs/api.html#model_Model.findOne
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003801%2fmulti-document-transactions-not-working-using-mongodb-atlas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I contacted the MongoDB Support and it turned out that this is a known issue:
We are currently aware of an issue with the M0 Free Tier clusters whereby multi-statement transactions timeout with an error. This should be fixed with the rollout of MongoDB version 4.0.5. In the meantime, if you require this feature urgently, I would recommend that you upgrade your cluster to an M10+ cluster.
So the issue occurs because I am using the free tier. But the bug will hopefully be fixed with the MongoDB 4.0.5 release.
UPDATE
As my database runs now on version 4.0.5, the problem is fixed. So it wasn't necessarily a issue with the code.
add a comment |
I contacted the MongoDB Support and it turned out that this is a known issue:
We are currently aware of an issue with the M0 Free Tier clusters whereby multi-statement transactions timeout with an error. This should be fixed with the rollout of MongoDB version 4.0.5. In the meantime, if you require this feature urgently, I would recommend that you upgrade your cluster to an M10+ cluster.
So the issue occurs because I am using the free tier. But the bug will hopefully be fixed with the MongoDB 4.0.5 release.
UPDATE
As my database runs now on version 4.0.5, the problem is fixed. So it wasn't necessarily a issue with the code.
add a comment |
I contacted the MongoDB Support and it turned out that this is a known issue:
We are currently aware of an issue with the M0 Free Tier clusters whereby multi-statement transactions timeout with an error. This should be fixed with the rollout of MongoDB version 4.0.5. In the meantime, if you require this feature urgently, I would recommend that you upgrade your cluster to an M10+ cluster.
So the issue occurs because I am using the free tier. But the bug will hopefully be fixed with the MongoDB 4.0.5 release.
UPDATE
As my database runs now on version 4.0.5, the problem is fixed. So it wasn't necessarily a issue with the code.
I contacted the MongoDB Support and it turned out that this is a known issue:
We are currently aware of an issue with the M0 Free Tier clusters whereby multi-statement transactions timeout with an error. This should be fixed with the rollout of MongoDB version 4.0.5. In the meantime, if you require this feature urgently, I would recommend that you upgrade your cluster to an M10+ cluster.
So the issue occurs because I am using the free tier. But the bug will hopefully be fixed with the MongoDB 4.0.5 release.
UPDATE
As my database runs now on version 4.0.5, the problem is fixed. So it wasn't necessarily a issue with the code.
edited Jan 10 at 18:23
answered Jan 9 at 8:31
FlorianFlorian
45423
45423
add a comment |
add a comment |
Try adding .session(session)
to each Query
const doc = await Schema2.findById(item.someId).session(session)
https://mongoosejs.com/docs/api.html#query_Query-session
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
add a comment |
Try adding .session(session)
to each Query
const doc = await Schema2.findById(item.someId).session(session)
https://mongoosejs.com/docs/api.html#query_Query-session
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
add a comment |
Try adding .session(session)
to each Query
const doc = await Schema2.findById(item.someId).session(session)
https://mongoosejs.com/docs/api.html#query_Query-session
Try adding .session(session)
to each Query
const doc = await Schema2.findById(item.someId).session(session)
https://mongoosejs.com/docs/api.html#query_Query-session
answered Jan 5 at 8:34
dmstackdmstack
1312
1312
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
add a comment |
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
Thanks for the suggestions! Unfortunately, after changing I get a new error (I updated my question)
– Florian
Jan 5 at 9:02
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I'm getting the same error as you for create/save transactions. Solutions in this discussion don't seem to do the trick either github.com/Automattic/mongoose/issues/6761
– dmstack
Jan 5 at 9:28
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I found this question: stackoverflow.com/questions/53254164 and it seems this bug can't be fixed
– Florian
Jan 5 at 9:32
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
I contacted the mongodb support. Maybe they will do something about it
– Florian
Jan 5 at 9:46
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
Just in case you didn't notice yet. The problem is fixed by upgrading to 4.0.5 (it's available now)
– Florian
Jan 10 at 18:36
add a comment |
Looks like you're missing a session option in findOne()
:
const doc = await Schema2.findById(item.someId, null, { session })
See: https://mongoosejs.com/docs/api.html#model_Model.findOne
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
add a comment |
Looks like you're missing a session option in findOne()
:
const doc = await Schema2.findById(item.someId, null, { session })
See: https://mongoosejs.com/docs/api.html#model_Model.findOne
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
add a comment |
Looks like you're missing a session option in findOne()
:
const doc = await Schema2.findById(item.someId, null, { session })
See: https://mongoosejs.com/docs/api.html#model_Model.findOne
Looks like you're missing a session option in findOne()
:
const doc = await Schema2.findById(item.someId, null, { session })
See: https://mongoosejs.com/docs/api.html#model_Model.findOne
answered Jan 3 at 17:08
vkarpov15vkarpov15
92484
92484
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
add a comment |
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
Sorry, but this didn't worked :(
– Florian
Jan 4 at 7:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003801%2fmulti-document-transactions-not-working-using-mongodb-atlas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I believe that if you use Promise.all you don't need async/await inside the map function. Try to remove it.
– Sagi Rika
Jan 2 at 9:51
Did not work :-(
– Florian
Jan 2 at 9:59
is createa an async function? or only find by id?
– Sagi Rika
Jan 2 at 10:00
create
returns aPromise
– Florian
Jan 2 at 10:02
1
I have version 4. I contacted the support and it's a known problem that will be fixed with version 4.0.5. Thank you anyways :)
– Florian
Jan 9 at 8:28