Mongoose unable to connect with Mongo Standalone replicaSet

Detail about the issue.

I have converted my MongoDB instance to a standalone ReplicaSet using the following config.

systemLog:  
  destination: file   
  path: /opt/homebrew/var/log/mongodb/mongo.log  
  logAppend: true  
storage:  
  dbPath: /opt/homebrew/var/mongodb  
net:  
  bindIp: 127.0.0.1  
replication:  
  replSetName: "rs0"  
security:  
    authorization: "enabled"  
    keyFile: /Users/username/mongodb.key   

Using the following Mongo URI to connect to my MongoDB instance

mongodb://username:password@127.0.0.1:27017/dbName?authSource=admin&replicaSet=rs0

using the URI I can connect to my MongoDB through Studio3T but when I use this URI in my Node app through Mongoose it never connects, nor gives any error.

Following are the Screenshots for rs.status() output displaying details of member
member data from rs.status


Additional details.
NodeJs version: v14.19.1
Mongoose version: 6.3.2
MongoDB version: 5.0.7

Hi @Shivam_Spraxa

It’s been a while since you posted this question. Are you still having issues with connecting using Mongoose?

If yes, what if you try to connect using a simple node script, e.g:

const MongoClient = require('mongodb').MongoClient;

let run = async function() {
  console.log('start')
  let opt = {poolSize:1, useNewUrlParser: true, useUnifiedTopology: true}
  let conn = await MongoClient.connect(
    'mongodb://username:password@127.0.0.1:27017/test?authSource=admin&replicaSet=rs0',
    opt)
  console.log('db connected')
  console.log(await conn.db('test').collection('test').findOne())
  conn.close()
  console.log('db closed')
}().catch(console.error)

The snippet above should try to connect to the database test, then print a single document from the collection test. If this snippet works, then we need to look deeper into why Mongoose have issues.

Best regards
Kevin

1 Like

A post was split to a new topic: Having a problem when trying to connect to replica set with compass and with python code