2

Using the Mongo Java API I can run the currentOp() command like this:

MongoClient mongoClient = null;
mongoClient = new MongoClient( "127.0.0.1", 27017);
db = mongoClient.getDB("admin");
db.command("currentOp");

But I only get details of current operations. I need to get details of idle connections too.

With reference with this
https://docs.mongodb.com/v3.0/reference/method/db.currentOp/#currentop-examples

Behavior

If you pass in true to db.currentOp(), the method returns information on all operations, including operations on idle connections and system operations.

db.currentOp(true) Passing in true is equivalent to passing in a query document of { '$all': true }.

If you pass a query document to db.currentOp(), the output returns information only for the current operations that match the query. You can query on the Output Fields. See Examples.

You can also specify { '$all': true } query document to return information on all in-progress operations, including operations on idle connections and system operations. If the query document includes '$all': true as well as other query conditions, only the '$all': true applies.

While using this command db.command("currentOp(true)");, I get an exception like this:

"ok" : 0.0 , "errmsg" : "no such command: 'currentOp(true)', bad cmd: '{ currentOp(true): true }'" , "code" : 59}

  • I'm still looking for response to this question. Spent some time on it, but couldn't do it for MongoDB 3.0. – Rav Apr 11 '17 at 15:56
0

Try this instead:

db.command("currentOp({$all: true})");

I've tested the JS version of this (don't have Java set up to test atm) and it worked. If you are looking for an example of how to do this, you can see the actual methods (rather than helpers) in Java in this example, compare the default on line 43 with "all" on line 49

0

I asked our MongoDB team to have a quick look seeing as it is Friday and nearly holidays. Here's their suggestion, hope it helps:

public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("192.168.88.11");
        MongoDatabase database = mongoClient.getDatabase("admin");
        Document currentOpResults = database.runCommand(new Document("currentOp", 1)).append("$all", true);
        System.out.println(currentOpResults.toJson());

    }

Gives output:

{ "inprog" : [{ "desc" : "conn1055", "threadId" : "139685894772480", 
"connectionId" : 1055, "client" : "192.168.88.102:57119", "active" : true,  
"opid" : 7664, "secs_running" : 0, "microsecs_running" : { "$numberLong" : 
"31" }, "op" : "command", "ns" : "admin.$cmd", "query" : { "currentOp" : 1 
}, "numYields" : 0, "locks" : { }, "waitingForLock" : false, "lockStats" : { 
} }], "ok" : 1.0, "$all" : true }

Code snippet:

/*
* using latest driver available at 
https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongo-java-driver/3.6.0/
* mongodb 3.4.1
*/

package javaapplication1;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class JavaApplication1 {

    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient(“servername.domain.com”);
        MongoDatabase database = mongoClient.getDatabase(“admin”);
        Document currentOpResults = database.runCommand(new Document(“currentOp”, 1)).append(“$all”, true);
        System.out.println(currentOpResults.toJson());

    }

}

For info, I work for Percona

  • The OP is trying to connect MongoClient with localhost with Loop back ip address(127.0.0.1) & MongoDB default port 27017. – Md Haidar Ali Khan Dec 15 '17 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.