Pymongo - Find primary/master node name in mongodb replica set

Viewed 342

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?

1 Answers

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