简体   繁体   中英

parse json output for primary and secondary hosts from replSetGetStatus

I've used pymongo to connect to mongo replica set and print the status of replica set using json dump. I want to parse this output and display "name" and "stateStr" into a list or array for the user to be able to pick a particular host.Here is my json dump output:

{

 { "replSetGetStatus": { "date": "2016-10-07T14:21:25", "members": [ { "_id": 0, "health": 1.0, "name": "xxxxxxxxxxx:27017", "optime": null, "optimeDate": "2016-10-07T13:50:11", "self": true, "state": 1, "stateStr": "PRIMARY", "uptime": 32521 }, { "_id": 1, "health": 1.0, "lastHeartbeat": "2016-10-07T14:21:24", "lastHeartbeatRecv": "2016-10-07T14:21:24", "name": "xxxxxxxxxxxx:27017", "optime": null, "optimeDate": "2016-10-07T13:50:11", "pingMs": 0, "state": 2, "stateStr": "SECONDARY", "syncingTo": "xxxxxxxxxxxx:27017", "uptime": 27297 }, { "_id": 2, "health": 1.0, "lastHeartbeat": "2016-10-07T14:21:24", "lastHeartbeatRecv": "2016-10-07T14:21:24", "name": "xxxxxxxxxxxxx:27020", "pingMs": 0, "state": 7, "stateStr": "ARBITER", "uptime": 32517 } ], "myState": 1, "ok": 1.0, "set": "replica1" } } 

Please try below Javascript code. It worked for me.

use admin;
var result = rs.status();
var length = result.members.length;
for (var i=0;i<length;i++){
        print ("Server Name-" +result.members[i].name);
        print ("Server State-" +result.members[i].stateStr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM