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?
Advertisement
Answer
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