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 🤔:
- Local MongoDB is not a Replica Set.
- Standalone MongoDB Instance: By default, MongoDB in a standalone configuration does not support multi-document transactions.
- 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()
🐧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
🎯 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.
Top comments (0)