Home > Software engineering >  Pymongo - Find primary/master node name in mongodb replica set
Pymongo - Find primary/master node name in mongodb replica set

Time:12-16

I'm working on an API which requires the primary node's name from an mongodb replia set.

Is there a way to get the name and ip of primary/master node in a replicaset using the pymongo API?

Or generally any way i could get that info from a python program?

CodePudding user response:

Use the hello command.

It will return

hello.primary
A string in the format of "[hostname]:[port]" listing the current primary member of the replica set.

For running a database command in pymongo see How to run raw mongodb commands from pymongo

  • Related