2

我使用 pymongo 连接到 mongo 副本集并使用 json 转储打印副本集的状态。我想解析这个输出并将“name”和“stateStr”显示到一个列表或数组中,以便用户能够选择一个特定的主机。这是我的 json 转储输出:

{

{
  "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"
  }
}

4

1 回答 1

0

请尝试下面的 Javascript 代码。它对我有用。

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);
}
于 2016-10-08T06:41:29.587 回答