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!

2
  • Some code would help to see what you've got this far Commented Jun 25, 2015 at 7:04
  • As of MongoDB Java driver version 3.0 and later (3.12+), use the database.runCommand() method to get the collection statistics document; see stackoverflow.com/questions/61052446/… Commented Apr 6, 2020 at 5:49

3 Answers 3

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"));
Sign up to request clarification or add additional context in comments.

2 Comments

is the Document, org.bson.Document? also could you link your answer or elaborate on what collStats, myCollection etc are
@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.
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"))

2 Comments

That's great! Work for me. Please update variable cursorDoc to resultSet.
Does anyone know how to do this with the MongoDB Java driver 3.2 without using these deprecated classes?
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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.