我在 node 中使用"mongoose": "^5.7.1"
.js项目.我正在制作一个api,其中包括两个文档的更新.因此,我使用如下事务:
// Start the transaction
session = await mongoose.startSession()
session.startTransaction()
await Promise.all([
<1st update operation>,
<2nd update operation>
])
// Commit the transaction
session.commitTransaction()
当我在本地环境中使用此api时,出现以下错误:
MongoError:此MongoDB部署不支持可重试写入.请在连接字符串中添加retryWrites=false.
当我在远程环境中使用这个api时,它运行良好.我使用https://www.clever-cloud.com作为数据库云,AWS作为api云.
如错误消息中所述,我已try 输入retryWrites=false
- 在我传递给mongoose作为
mongodb://${ip}:${port}/${this.MONGO_DATABASE}?retryWrites=false
的连接字符串的末尾 - 将选项作为
retryWrites: false
传递给mongoose.connect
方法.
mongoose.connect(`mongodb://${ip}:${port}/${this.MONGO_DATABASE}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
retryWrites: false
}, (err) => {...})
以上这些都没有解决这个问题.
下面是mongo --version
命令的输出:
db version v4.0.13
git version: bda366f0b0e432ca143bc41da54d8732bd8d03c0
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
我进行了调试,发现抛出此错误背后的实际错误是:
MongoError:事务号只允许在副本集成员或mongos上使用
请提些建议.