Joey Yi Zhao
Joey Yi Zhao

Reputation: 42644

How to use Mongo Java Driver to perform some kind of admin command?

In Java Driver of MongoDB, does it support reading mongo shell command directly? I am creating a Mongo client for Mongo dba in Java and want to allow users to type any kind of Mongo shell commands but I am not sure whether it is possible to do all dba tasks through Mongo Java Driver. For example, when users type show dbs or show collections in mongo shell, a list of databases or collections will be shown. Also dba can type rs.status(), rs.config() commands to execute some admin tasks. I wonder whether mongo java driver supports that kind of input. I know I can use some API from the Java driver like database.runCommand(new Document(...)); but it needs some translating tasks between shell command and Java methods. Whether is there a better way to do that?

Upvotes: 1

Views: 718

Answers (1)

satish chennupati
satish chennupati

Reputation: 2650

 protected CommandResult runReplicaSetStatusCommand(final Mongo pMongo) {
        // Check to see if this is a replica set... if not, get out of here.
        final CommandResult result = pMongo.getDB("admin").command(
                new BasicDBObject("replSetGetStatus", 1));

        final String errorMsg = result.getErrorMessage();

        if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
            System.err
                    .println("This is not a replica set - not testing secondary reads");
            return null;

get full example at : http://massapi.com/source/bitbucket/12/14/1214103355/src/test/java/com/mongodb/util/TestCase.java.html#307

Upvotes: 1

Related Questions