使用mLab进行MongoDB写入时出现”MongoError:此MongoDB部署不支持可重试写入”的错误

简而言之

有一项名为mLab MongoDB的服务在云端提供MongoDB。当进行数据写入时,出现了MongoError: This MongoDB deployment does not support retryable writes的错误。下面是解决方法的备忘录。

导致这个的原因是什么?

根据这篇Stack Overflow的文章,mLab无法使用MongoDB的连接设置retryWrites选项,因此需要将其设为false。
retryWrites选项是在写入发生错误时,数据库会自动尝试重新写入的功能。根据官方手册,MongoDB 4.2驱动默认为true。

连接设置时的配置

以下是在使用Rails的Mongoid时将retryWrites选项设置为false的配置。有关mongoid.yml配置文件的详细信息,请参考mongoid-configuration。

production:
  clients:
    default:
      database: heroku_test
      hosts:
        - test111111.mlab.com:51180
      options:
        user: "test_user"
        password: "test_password"
        retry_writes: false
  options:
广告
将在 10 秒后关闭
bannerAds