2017-03-09 46 views
1

爲了實現後述的外殼命令如何使用API​​

replicaset運行蒙戈數據庫db.currentOp(真)命令:PRIMARY> db.currentOp()

Am 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

行爲

如果您將true傳遞給db.currentOp(),則方法返回所有操作的信息,包括空閒連接和系統操作的操作。

db.currentOp(true)傳入true表示傳入查詢文檔{'$ all':true}。

如果將查詢文檔傳遞給db.currentOp(),則輸出將僅返回與查詢匹配的當前操作的信息。您可以在輸出字段上進行查詢。參見示例。

您還可以指定{'$ all':true}查詢文檔以返回所有正在進行的操作的信息,包括空閒連接和系統操作的操作。如果查詢文檔包含'$ all':true以及其他查詢條件,則僅應用'$ all':true。

在蒙戈命令外殼,我們有方法類似

replicaset:PRIMARY> db.currentOp(真)

While using this command in JAVA API code  
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} 

請提出一個解決方案

回答

0

你可以試試下面的java碼。

CommandResult result = db.command(new BasicDBObject("currentOp", 1).append("$all", true)); 

或者

CommandResult result = db.command(new BasicDBObject("currentOp", true));