Issue in connecting mongodatabase

53 Views Asked by At

I am using mongoose version 4.13.6 and mongodb from compose, and below is my code for connecting to mongo database.

mongoose.createConnection('mongodb://[user]:[pass]@[host1]:[port1],[host2]:[port2]/dbnamme?ssl=true', {});

But when I run this am getting error,

MongoError: no primary found in replicaset

Dont know why is that, can anyone help me in this?

1

There are 1 best solutions below

0
On

So the short answer is this:

... all drivers are not equal and some make assumptions when multiple hosts are specified. For example, the Meteor/Node.js MongoDB driver sees two hosts and assumes it is talking to a replicaset. Upon connecting the driver asks which host is master and then errors out because neither of them are. The simple fix for this is to use one host in the URI ..

https://www.compose.com/articles/connecting-to-the-new-mongodb-at-compose/#drivingtoyourfirstdatabase

So when you create a connection, simply use one of the connection URIs for the database you want to connect to like:

var uri = "mongodb://<username>:<password>@[host]:[port]/<db_name>?ssl=true";

mongoose.createConnection(uri);