Search code examples
mongodbmongoose

MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string


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()

When I hit this api on my local environment, I get following error:

MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.

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.

As written in error message, I have tried to put retryWrites=false

  • 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 the mongoose.connect method.
mongoose.connect(`mongodb://${ip}:${port}/${this.MONGO_DATABASE}`, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    retryWrites: false
  }, (err) => {...})

None of the above resolved the issue.

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 are only allowed on a replica set member or mongos

Please suggest something.


Solution

  • 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.

    I have used run-rs for this purpose.