Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 374
Andro Developer Avatar asked Oct 28 '19 10:10

Andro Developer


People also ask

Does MongoDB support retryable writes and reads?

The official MongoDB 4.2-series drivers enable retryable writes by default. Applications which write to the local database will encounter write errors upon upgrading to 4.2-series drivers unless retryable writes are explicitly disabled.

What is MongoDB SRV?

MongoDB URI syntax. . The use of SRV records eliminates the requirement for every client to pass in a complete set of state information for the cluster. Instead, a single SRV record identifies all the nodes associated with the cluster (and their port numbers) and an associated TXT record defines the options for the URI ...

What is MongoDB transaction?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.


2 Answers

As suggested in accepted answer, you need to have your local server to be run as a replica set to be able to perform transactions, as opposed to standalone server.

However, in addition to the proposed solution, you can easily convert your Standalone local db to a Replica Set without using any third-party tool, by following instructions in MongoDB documentation, summarized as follows:

  1. Stop your standalone mongod instance, and restart it with the replSet argument.
mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost 
  1. Connect to your instance with a mongo shell, and initiate the new Replica Set.
rs.initiate() 

Now you should have a Replica Set instead of a Standalone mongodb server, where you can perform transactions on your local environment to update multiple documents at once!

Do not forget to include the replSet argument every time you want to start the server, otherwise it will be started as Standalone. I simply use the same command as in step 1 to run it again.


Alternatively, you can deploy a new Replica Set from scratch for testing environment following these other instructions in MongoDB documentation.

like image 93
gasbi Avatar answered Sep 23 '22 10:09

gasbi


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.

like image 25
Andro Developer Avatar answered Sep 23 '22 10:09

Andro Developer