question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shouldn't mongoose detect primary in a replica set?

See original GitHub issue

Using mongoose 3.8.8.

Not sure if this is a “bug” or if I’ve misunderstood how Mongoose should handle replica sets, but here it goes:

I’m using a replica set with one primary and one secondary (hosted by MongoHQ) and connecting using mongoose.connect('mongodb://user:pass@hello.com:12345/my_db,mongodb://user:pass@hello.com:54321/my_db') (with other URI:s and credentials of course).

The thing is that mongoose can’t write to the database unless I specify the primary first in the URI string (it attempts to write to the secondary, which it can’t for obvious reasons). I was under the impression that mongoose would try to figure out which one in a replica set that is the primary? Otherwise, it would require a new code deployment if we were to change primary?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18

github_iconTop GitHub Comments

2reactions
troyfactor4commented, Feb 11, 2015

I know this is late but:

Just dealt with this issue for the last day and a half. We changed db.open(‘…’) to db.openSet(‘…’) and all works fine. Using the openSet function is specific to mongoose and sets the internal replica variable to true. When not using openSet, only the first mongodb in the list is used and the others are stripped away.

Hope this helps someone.

0reactions
vkarpov15commented, Jul 17, 2015

I’m not an expert but I’d imagine connecting through DNS round robin would not work terribly well. The node driver doesn’t cache server IP addresses, so if it has 3 hosts all at mongo.marathon.mesos, it will continue to make requests to mongo.marathon.mesos for isMaster requests (essentially mongodb’s heartbeats) so you’d have to hope that the driver is making requests in exactly the right order. I’d recommend not using DNS round robin with the node driver.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Mongoose - Get replica set primary - Stack Overflow
I have a few mongodb instances running as a replica set and I am connected to that set using mongoose . I would...
Read more >
Replica Set Elections — MongoDB Manual
When the primary detects that it can only see a minority of nodes in the replica set, the primary steps down as primary...
Read more >
How To Configure a MongoDB Replica Set on Ubuntu 20.04
Each replica set can have only one primary member at a time, as replication happens by copying the primary's oplog (short for “operations...
Read more >
How to deploy a MongoDB Replica Set using Docker
You are free to add nodes to this replica set at any time. You can even stop one of the mongo container or...
Read more >
How To Create a MongoDB Replica Set - Linode
Replica sets also fail over automatically, so if one of the members becomes unavailable, a new primary host is elected and your data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found