Shouldn't mongoose detect primary in a replica set?
See original GitHub issueUsing 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:
- Created 9 years ago
- Comments:18
Top GitHub Comments
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.
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 tomongo.marathon.mesos
forisMaster
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.