Mongodb v4.0 Transaction, MongoError: Transaction numbers are only allowed on a replica set member or mongos

I've installed MongoDB v4.0 for the most amazing feature of it Transaction in Nodejs with mongodb 3.1 as a driver.

When I try to use a transaction session I've faced this error:

MongoError: Transaction numbers are only allowed on a replica set member or mongos.

What's that and how can I get rid of it?

Any suggestion is appreciated.


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.

In order to use transactions, you need a MongoDB replica set, and starting a replica set locally for development is an involved process. The new run-rs npm module makes starting replica sets easy. Running run-rs is all you need to start a replica set, run-rs will even install the correct version of MongoDB for you.

Run-rs has no outside dependencies except Node.js and npm. You do not need to have Docker, homebrew, APT, Python, or even MongoDB installed.

Install run-rs globally with npm's -g flag. You can also list run-rs in your package.json file's devDependencies.

npm install run-rs -g

Next, run run-rs with the --version flag. Run-rs will download MongoDB v4.0.0 for you. Don't worry, it won't overwrite your existing MongoDB install.

run-rs -v 4.0.0 --shell

Then use replicaSet=rs in your connection string.

You find more details about it here.


I got the solution, and it's just three lines configuration inside the MongoDB config file.

After switching from MongoDB atlas and installing MongoDB v 4.4.0 on my CentOS 7 VPS with WHM, I faced that issue also.

the run-rs solution does not work for me, but I managed to solve this issue without any third-party tool, following these steps:

1. turn off mongod.

the most efficient way is by entering the MongoDB shell with the command mongo checkout the method

db.shutdownServer()

You will be no ability to use the MongoDB server. For me, the shutdown process took too long, and then I killed the process with the command:

systemctl stop -f mongod

if you killed the mongod process,s probably you will need to run mongod --dbpath /var/db --repair

The var/db should point to your database directory.

2. setting replicaSet configuration.

for the replicaSet settings step, check out the /etc/mongod.conf file, look for the replication value line, and you should add the following lines as below:

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>

use the replSetName value on the next step.

an example of those settings:

   oplogSizeMB: 2000
   replSetName: rs0
   enableMajorityReadConcern: false

3. add your connection string URL.

add the value of replSetName to your connection URL &replicaSet=--YourReplicationSetName--

if you used the name rs0 from our example, then you should add to your DB connection URL query replicaSet=rs0

4. turn on mongod again

enter the command: systemctl start mongod

5. Access your replicaSet database

enter MongoDB shell with the command mongo, enter the command rs.initiate() now you should be in your replicaSet database.


I faced the same issue recently. In my case it's because I'm connecting to a remote Mongo server with a different version than my local development environment.

To quickly solve the issue, I added the following param to my connection string:

?retryWrites=false


Possible solution for local development using docker

Create Dockerfile

FROM mongo:4.4.7
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "rs" ]

Build this Dockerfile

docker build ./ -t mongodb:4.7-replset

Run this created image

docker run --name mongodb-replset -p 27017:27017 -d mongodb:4.7-replset

Connect to database using this URI

mongodb://localhost:27017/myDB

In order to use transactions, you need a MongoDB replica set, and starting a replica set locally for development is an involved process.

You can use the run-rs npm module. Zero-config MongoDB runner. Starts a replica set with no non-Node dependencies, not even MongoDB.

Or you can simply create an account in MongoDB Atlas which gives you a limited resource MongoDB cluster and so you can run/test your application.

MongoDB Atlas