Andro Developer
Andro Developer

Reputation: 1573

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

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.

Upvotes: 34

Views: 49036

Answers (4)

kris
kris

Reputation: 12580

Please try adding &retryWrites=false to your connection string.


I actually got the errors mentioned in the OP when connecting to our remote db server, whereas it was working locally. I contacted our mongo hosted support before trying the suggestion that is in the error.


This is what our hosted mongo site (mLab) said :

It's likely your app's driver was updated to a more recent version which is attempting to use a WiredTiger-only feature. As the error mentions, you'll need to add &retryWrites=false to your connection string.

https://docs.mlab.com/faq/#why-am-i-getting-the-transaction-numbers-are-only-allowed-on-storage-engines-that-support-document-level-locking-error

Upvotes: 6

gasbi
gasbi

Reputation: 848

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.

Upvotes: 25

Andro Developer
Andro Developer

Reputation: 1573

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.

Upvotes: 7

Arshit
Arshit

Reputation: 136

Please Edit App/Config/database file Add 'retryWrites'=>false in Mongodb Connection String

write Mongo db connection

'mongodb' => [
            'driver'   => 'mongodb',
            'host'     => env('MONGO_DB_HOST', 'lo*****'),
            'port'     => *****,
            'database' => env('MONGO_DB_DATABASE'),
            'username' => env('MONGO_DB_USERNAME'),
            'password' => env('MONGO_DB_PASSWORD'),
            'options'  => [
                'database'=> env('MONGO_DB_DATABASE'),
                'retryWrites'=>false
            ]
        ],

Save And Run

Upvotes: 2

Related Questions