mongoose: Shouldn't mongoose detect primary in a replica set?

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?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 18

Most upvoted 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.