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

3

4


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!

Turnout answered 25/6, 2015 at 7:2 Comment(2)
Some code would help to see what you've got this farOurs
As of MongoDB Java driver version 3.0 and later (3.12+), use the database.runCommand() method to get the collection statistics document; see #61052946Arianism
K
4

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"))
Knighten answered 25/6, 2015 at 7:17 Comment(2)
That's great! Work for me. Please update variable cursorDoc to resultSet.Turnout
Does anyone know how to do this with the MongoDB Java driver 3.2 without using these deprecated classes?Addle
Z
10

@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"));
Zook answered 27/5, 2016 at 15:3 Comment(2)
is the Document, org.bson.Document? also could you link your answer or elaborate on what collStats, myCollection etc areBygone
@nullpointer - Ok, I wanted to keep this as small and concise as possible and assumed people would just google the official doc for these questions, but sure... 1. Yes, it is org.bson.Document 2. For collStats please see: docs.mongodb.com/manual/reference/command/collStats 3. 'myCollection' refers to the name of the mongo collection you want to run 'collStats' (see above) on Basically this is just workable Java syntax for officially documented mongo commands. Hope that helps.Zook
K
4

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"))
Knighten answered 25/6, 2015 at 7:17 Comment(2)
That's great! Work for me. Please update variable cursorDoc to resultSet.Turnout
Does anyone know how to do this with the MongoDB Java driver 3.2 without using these deprecated classes?Addle
D
1

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}
Decoteau answered 25/6, 2015 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.