Connect to a secondary mongodb server for reading

Hi,

Anyone ever tried connecting to a secondary mongodb server using MongoInternals.RemoteCollectionDriver and do some reads? I’ve tried different connection strings and options but still I’m unable to make it work. I’ve dug deep through meteor code and seems url and options are being passed correctly to mongo driver. Not sure where the issue is.

Here’s a few connection strings I’ve tried:
mongodb://seconday.server:27017/dbname?slaveOk=true
mongodb://seconday.server:27017/dbname?readPreference=secondary
mongodb://primary.server:27017,seconday.server:27017/dbname?replicaSet=myset&readPreference=secondary

Also tried passing options to MongoInternals.RemoteCollectionDriver:
{ readPreference: “secondary”}
{ readPreference: “secondary”, slaveOk: true}
{ db: {readPreference: “secondary”}}

I’ve also tried combinations of both but still didn’t.

Error I’ve got for the connection string with replicaSet:
MongoError: no connection available

Errors for the rest is usually:
MongoError: not master and slaveOk=false

I’m using versions below:
Meteor: 1.5.2.2
MongoDB: 3.4 (both primary and secondary)

Pointing me to the right direction would be a big help.

Thanks!

3 Likes

Hi @ianfynd,

I’m trying the same thing and finding the same issues as you.
Did you ever manage to make it work? Or find a workaround?

Thank you

@nelsonuruworks unfortunately no. I ended up connecting directly to the primary. Please let me know if you find a solution.

There isn’t anything theoretically wrong with connecting to a secondary when you specify a readpreference and have the list of hosts.

The bug is probably https://github.com/meteor/meteor/issues/9026

Hi @nelsonuruworks @ianfynd did you manage to make it work ?

Thanks

Hi @jadus do you have a problem with this? I connect in multiple platforms to multiple Mongo (Atlas) and in all I “cross connect”. DBs. You can connect into as many DBs as you want in 2 concepts:

  • initiate connection and remain connected (normal Meteor behavior).
  • open connection, do transaction, close connection.
    Let me know if I can assist in any way.

Hi @paulishca, thanks for your help. A Mongodb consultant told me to add “readPreference=secondary” to my mongo connexion string (settings.json galaxy.meteor.com env MONGO_URL) in order to improve performance but I wanted to be sure that meteor can handle this

If you update a lot of data and looking to millisecond performance, primaryPreferred with hedged reads might yield more precise query responses.
With .rawCollection you can use all Mongo official options.