Pymongo - how to get python dictionary status for rep sets

I am new to mongo and I use pymongo. I will find documentation for pymongo to be everywhere.

1) I have a set of repressions. From the mongo shell, if I run below, I get what I need.

sudo mongo 111.111.111.111 --eval "printjson(rs.status())" MongoDB shell version: 2.2.1 connecting to: 111.111.111.111/test { "set" : "hey", "date" : ISODate("2012-11-10T11:47:58Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "111.111.111.111:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 69189, "optime" : Timestamp(1352478921000, 1), "optimeDate" : ISODate("2012-11-09T16:35:21Z"), "self" : true } ], "ok" : 1 } 

I need to get this information from pymongo.

1) I connect to the primary. I get none , but in the above example, node is the main one:

 c = ReplicaSetConnection("111.111.111.111:27017", replicaSet='heythat') print c.primary None 

2) I want to run c.command("status") , but this operation is not supported.

So, how can I use pymongo to return primary as well as return status to a Python dictionary?

+6
source share
1 answer

rs.status() calls the admin command 'replSetGetStatus', in python you can do this:

 conn = ReplicaSetConnection("111.111.111.111:27017", replicaSet='heythat') conn.admin.command('replSetGetStatus') 

Protip: If you want to know which command calls the shell call, then leave the parentheses from the function to see the code, for example:

 heythat:PRIMARY> rs.status function () { return db._adminCommand("replSetGetStatus"); } 
+9
source

All Articles