0

I know we can connect to replica set using pymongo and it can automatically figure out which one is primary under normal circumstances. However, our replica set is running inside of containers, therefore, the hostname registered in the replica set configuration are not pingable outside of containers. I can only access the instance on a per node basis by supplying the IP of the host where the container reside. My question arise from the need to figure out if the node I connect to is indeed primary, I know to check that under mongo shell, I could do either rs.isMaster() or db.isMaster().ismaster, but neither seems to work with my client connection via pymongo.

Hopefully my question is clear enough, if not please leave a comment and I can try to elaborate, any help is greatly appreciated. Thanks in advance. In the meantime, I will keep trying other things.

JDA
  • 1
  • 1
  • It is possible to get this to work "properly". Start with setting your connection string to a comma separated list of IPs e.g. `mongodb://:@10.0.0.10,10.0.0.11,10.0.0.12/test?replicaSet=int&authSource=admin`. Once done you may need to tweak your hosts file so that the IPs match with the hostnames mongodb expects. – Belly Buster May 26 '20 at 14:38
  • `db.isMaster()` is a javascript helper. From python, run the `isMaster` command, see https://stackoverflow.com/a/27298423/2282634 – Joe May 27 '20 at 01:22

0 Answers0