DEV Community

Cover image for ⛔MongoDB Transactions Error: "Transaction numbers are only allowed on a replica set member or mongos
Sarwar Hossain
Sarwar Hossain

Posted on

1

⛔MongoDB Transactions Error: "Transaction numbers are only allowed on a replica set member or mongos

If you're using Transaction rollback with Mongoose in a Node.js application and encounter the error "Transaction numbers are only allowed on a replica set member or mongos".

Reasons for the Error 🤔:

  1. Local MongoDB is not a Replica Set.
  2. Standalone MongoDB Instance: By default, MongoDB in a standalone configuration does not support multi-document transactions.
  3. Missing Transaction Support: Transactions are only available in replica set members or in clusters managed by mongos (sharded MongoDB).

🖥️ Solution Steps (On Windows):

stop your mongodb service and edit the C:\Program Files\MongoDB\Server{version}\bin\mongod.cfg file

# ⏹️ stop the service
net stop MongoDB

# 📝 edit below file
C:\Program Files\MongoDB\Server\{version}\bin\mongod.cfg

# set replica 
replication:
  replSetName: "rs0" # any name can set

#  🔄 start the service

net start MongoDB

# 🔧 open mongo SHell  & init the the rs

rs.initiate()

# check the status
rs.status()


Enter fullscreen mode Exit fullscreen mode

🐧Solution Steps (On Linux/Ubuntu server):

#⏹️  mongodb service stop
sudo service mongod stop

#🔧 access the mongod.config
 sudo chmod -R 777 /etc/mongod.conf

#edit the cofigurationh
 nano /etc/mongod.conf

 #add now

# 🔧 Mongo db replica for Mongoose Transaction rollback.

replication:
  replSetName: "rs0"

 #  🔄 start the service
 sudo service mongod start


# now go to mongo shell
mongosh

# now update the 
rs.initiate()

# verify  status

 rs.status()

 # start the mongod

 sudo service mongod start

Enter fullscreen mode Exit fullscreen mode

🎯 Conclusion:

By following these steps, you'll be able to fix the "Transaction numbers are only allowed on a replica set member or mongos" error and leverage MongoDB transactions effectively in your Node.js application on Windows or Linux.

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay