dondon
dondon

Reputation: 105

MongoDB run command replSetGetStatus from Java driver

I want to retrieve the detailed status of each member of a replica set in Java driver as a BSON document and then process that document. However, I am having problem with the retrieving step. My code is as following:

MongoClient shard = new MongoClient(new MongoClientURI("mongodb://" + shardUri));

BasicDBObject replStatCmd = new BasicDBObject("replSetGetStatus", 1);
Document replStatus = shard.getDatabase("admin").runCommand(replStatCmd);

System.out.println(replStatus);

I can run this command in the admin database of Mongo shell. But my implementation in Java returns nothing as document and the code never gets to the line println(). Can anyone give me some suggestions ?

Upvotes: 2

Views: 1764

Answers (1)

Anirudh Bagri
Anirudh Bagri

Reputation: 2447

You can try out this,

DB db = shard.getDatabase("admin");
DBObject cmd = new BasicDBObject();
cmd.put("replSetGetStatus", 1);
CommandResult result = db.command(cmd);

Or in newer API version, use Document class is from package org.bson

DB db = shard.getDatabase("admin");
Document documentA = db.runCommand(new Document("replSetGetStatus",1));

Upvotes: 1

Related Questions