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

2

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

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

Top comments (1)

Collapse
 
srikanth209 profile image
srikanth

Thanks for this, helped.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay