How to get data for printReplicationInfo using Java or Node JS?

I want to get the output of printReplicationInfo or getReplicationInfo using Java or Node JS but I could not get can any one help me out.

Welcome to the MongoDB community @Pradeep_kumar.K!

The db.printReplicationInfo() and db.getReplicationInfo() shell helpers are implemented in JavaScript in the mongo shell. These administrative helpers aren’t part of the standard driver API, but you can implement the same logic using any driver.

Since these helper functions are written in JavaScript in the mongo shell, you can invoke the helpers without parentheses to see the underlying JavaScript code.

For example:

> db.printReplicationInfo
function() {
    var result = this.getReplicationInfo();
    if (result.errmsg) {
        var isMaster = this.isMaster();
        if (isMaster.arbiterOnly) {
            print("cannot provide replication status from an arbiter.");
            return;
        } else if (!isMaster.ismaster) {
            print("this is a secondary, printing secondary replication info.");
            this.printSecondaryReplicationInfo();
            return;
        }
        print(tojson(result));
        return;
    }
    print("configured oplog size:   " + result.logSizeMB + "MB");
    print("log length start to end: " + result.timeDiff + "secs (" + result.timeDiffHours + "hrs)");
    print("oplog first event time:  " + result.tFirst);
    print("oplog last event time:   " + result.tLast);
    print("now:                     " + result.now);
}

From this source example you can see that db.printReplicationInfo() uses data from db.getReplicationInfo() and the output of the isMaster command.

If you don’t want to reimplement this functionality in your preferred language driver, another option would be to capture mongo output using your language’s shell exec equivalent to call something like mongo --eval "db.printReplicationInfo()" --quiet.

Regards,
Stennie

Hello @Pradeep_kumar.K,

Based upon @Stennie 's note, here is a way to run the db.printReplicationInfo() command using Java code. The Java code basically runs the command mongo.exe --eval=db.printReplicationInfo() --quiet from the OS command line and prints the output.

private static void doCommand() 
		throws IOException, InterruptedException {

	final String [] cmd = { "mongo.exe", "--port=30001", "--eval=db.printReplicationInfo()", "--quiet" };

	ProcessBuilder ps = new ProcessBuilder(cmd);
	ps.redirectErrorStream(true);
	Process pr = ps.start();
	
	try(BufferedReader in = new BufferedReader(
			new InputStreamReader(pr.getInputStream()))) {
		String line;
		while ((line = in.readLine()) != null) {
			System.out.println(line);
		}
		pr.waitFor();
		System.out.println("Done.");
	}
}

For the above code I got an output like this:

configured oplog size:   990MB
log length start to end: 251secs (0.07hrs)
oplog first event time:  Tue Jan 05 2021 12:42:54 GMT+0530 (India Standard Time)
oplog last event time:   Tue Jan 05 2021 12:47:05 GMT+0530 (India Standard Time)
now:                     Tue Jan 05 2021 12:47:15 GMT+0530 (India Standard Time)
Done.

Just substitute the cmd variable for the db.getReplicationInfo() command or any other command you want to run. You can also use other parameters like --port as per your requirement.

I am getting below error.

java.io.IOException: Cannot run program “mongo.exe”: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at TestMongo.doCommand(TestMongo.java:1517)
at TestMongo.main(TestMongo.java:1539)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)

Instead of the “mongo.exe” try using the complete path to it, e.g., “C:\mongodb-server-4.2.8\bin\mongo.exe” in the variable cmd.

This works thanks for the help.