简体   繁体   中英

How to call db.Collection.stats() from Mongo java driver


From Mongodb client, we can use db.Collection.stats() to get status of collections, such as:
+ Number of records (count)
+ Size on disk (storageSize)
+ Indexes (indexSizes)
+ Average object size (avgObjSize)

Now I want to monitor these data from web backend with Mongodb java driver, please let me know how to get them?

I've referred: http://mongodb.github.io/mongo-java-driver/3.0/driver-async/getting-started/quick-tour-admin/ but it's not enough information for me.

Thanks!

@Yoshiya (sorry, don't have enough rep for comment permission)

This works for me on 3.2 driver (kindly provided by Kay Kim from the Mongo folks)

MongoDatabase database = mongoClient.getDatabase("mydb");
Document stats = database.runCommand(new Document("collStats", "myCollection"));

Use CommandResult to finding collection stat in java check below code :

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("data base name");
CommandResult resultSet = db.getCollection("collectionName").getStats();
System.out.println(resultSet);
System.out.println(resultSet.get("count"));
System.out.println(resultSet.get("avgObjSize"))

This will work:

CommandResult resultSet = db.getCollection("emp").getStats();
        System.out.println(resultSet);

you will get all the details of status.ouput:

{ "ns" : "test.emp" , "count" : 2 , "size" : 96 , "avgObjSize" : 48 , "numExtents" : 1 , "storageSize" : 8192 , "lastExtentSize" : 8192.0 , "paddingFactor" : 1.0 , "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only." , "userFlags" : 1 , "capped" : false , "nindexes" : 1 , "indexDetails" : { } , "totalIndexSize" : 8176 , "indexSizes" : { "_id_" : 8176} , "ok" : 1.0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM