MongoError:此 MongoDB 部署不支持可重试写入.请将 retryWrites=false 添加到您的连接字符串 [英] MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string
问题描述
我在我的 Node.js 项目中使用 "mongoose": "^5.7.1"
.我正在制作一个涉及更新两个文档的 api.所以,我正在使用如下交易:
I am using "mongoose": "^5.7.1"
in my Node.js project. I am making an api which involves updating in two documents. So, I am using the transactions like following:
// Start the transaction
session = await mongoose.startSession()
session.startTransaction()
await Promise.all([
<1st update operation>,
<2nd update operation>
])
// Commit the transaction
session.commitTransaction()
当我在本地环境中点击这个 api 时,出现以下错误:
When I hit this api on my local environment, I get following error:
MongoError:此 MongoDB 部署不支持可重试写入.请将 retryWrites=false 添加到您的连接字符串中.
MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
当我在远程环境中点击这个 api 时,它运行良好.我使用 https://www.clever-cloud.com 作为数据库云和 AWS 作为 api 云.
When I hit this api on remote environment, then it runs fine. I am using https://www.clever-cloud.com as database cloud and AWS as api cloud.
如错误消息中所写,我尝试将 retryWrites=false
As written in error message, I have tried to put retryWrites=false
- 在我作为
mongodb://${ip}:${port}/${this.MONGO_DATABASE}?retryWrites=false
传递给猫鼬的连接字符串的末尾> - 将选项作为
retryWrites: false
传递给mongoose.connect
方法.
- at the end of connection string that I am passing to mongoose as
mongodb://${ip}:${port}/${this.MONGO_DATABASE}?retryWrites=false
- with options as
retryWrites: false
passed to themongoose.connect
method.
mongoose.connect(`mongodb://${ip}:${port}/${this.MONGO_DATABASE}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
retryWrites: false
}, (err) => {...})
以上都没有解决问题.
以下是 mongo --version
命令的输出:
Below is the output of mongo --version
command:
db version v4.0.13
git version: bda366f0b0e432ca143bc41da54d8732bd8d03c0
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
我已经调试并发现抛出此错误背后的实际错误是:
I have debug and find the actual error behind throwing this error is:
MongoError: Transaction numbers 只允许在副本集成员或 mongos 上
MongoError: Transaction numbers are only allowed on a replica set member or mongos
请提出建议.
推荐答案
事务无疑是 MongoDB 4.0 中最令人兴奋的新特性.但不幸的是,大多数用于安装和运行 MongoDB 的工具都会启动一个独立的服务器,而不是副本集.如果您尝试在独立服务器上启动会话,则会收到此错误.
Transactions are undoubtedly the most exciting new feature in MongoDB 4.0. But unfortunately, most tools for installing and running MongoDB start a standalone server as opposed to a replica set. If you try to start a session on a standalone server, you'll get this error.
可以通过在本地环境中使用副本集来解决此问题.
This issue can be resolved by using replica-sets on your local environment.
为此目的,我使用了 run-rs.
I have used run-rs for this purpose.
这篇关于MongoError:此 MongoDB 部署不支持可重试写入.请将 retryWrites=false 添加到您的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!