getReplicationInfo command

526 views
Skip to first unread message

majeed said

unread,
Apr 12, 2016, 3:21:38 AM4/12/16
to mongodb-user
Hi,

 when running the command getReplicationInfo command in java (driver 3.2) as the following :

"localDB.runCommand(new Document("getReplicationInfo", Boolean.TRUE));"

getting the error that command is unknown:

com.mongodb.MongoCommandException: Command failed with error 59: 'no such command: getReplicationInfo' on server xxxxxxxxx:27017. The full response is { "ok" : 0.0, "errmsg" : "no such command: getReplicationInfo", "code" : 59, "bad cmd" : { "getReplicationInfo" : true } }


can u please advise how can run the mongo command in java??

Regards,
Majeed

Ross Lawley

unread,
Apr 18, 2016, 4:04:01 AM4/18/16
to mongod...@googlegroups.com
Hi Majeed,

The reason you are getting an error is because `db.getReplicationInfo()` is not a command.  In fact in its a function in the shell, you can print the function in the shell by removing the parentheses, which gives:

MongoDB Enterprise > db.getReplicationInfo
function () {
    var localdb = this.getSiblingDB("local");

    var result = { };
    var oplog;
    var localCollections = localdb.getCollectionNames();
    if (localCollections.indexOf('oplog.rs') >= 0) {
        oplog = 'oplog.rs';
    }
    else if (localCollections.indexOf('oplog.$main') >= 0) {
        oplog = 'oplog.$main';
    }
    else {
        result.errmsg = "neither master/slave nor replica set replication detected";
        return result;
    }

    var ol = localdb.getCollection(oplog);
    var ol_stats = ol.stats();
    if( ol_stats && ol_stats.maxSize ) {
        result.logSizeMB = ol_stats.maxSize / ( 1024 * 1024 );
    } else {
        result.errmsg  = "Could not get stats for local."+oplog+" collection. " +
            "collstats returned: " + tojson(ol_stats);
        return result;
    }

    result.usedMB = ol_stats.size / ( 1024 * 1024 );
    result.usedMB = Math.ceil( result.usedMB * 100 ) / 100;

    var firstc = ol.find().sort({$natural:1}).limit(1);
    var lastc = ol.find().sort({$natural:-1}).limit(1);
    if( !firstc.hasNext() || !lastc.hasNext() ) {
        result.errmsg = "objects not found in local.oplog.$main -- is this a new and empty db instance?";
        result.oplogMainRowCount = ol.count();
        return result;
    }

    var first = firstc.next();
    var last = lastc.next();
    var tfirst = first.ts;
    var tlast = last.ts;

    if( tfirst && tlast ) {
        tfirst = DB.tsToSeconds( tfirst );
        tlast = DB.tsToSeconds( tlast );
        result.timeDiff = tlast - tfirst;
        result.timeDiffHours = Math.round(result.timeDiff / 36)/100;
        result.tFirst = (new Date(tfirst*1000)).toString();
        result.tLast  = (new Date(tlast*1000)).toString();
        result.now = Date();
    }
    else {
        result.errmsg = "ts element not found in oplog objects";
    }

    return result;
}

So you'd need to replicate the whole function and its logic in the java driver.

Ross

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/aa4ae123-0d21-478d-9deb-a0d26056468c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

Reply all
Reply to author
Forward
0 new messages