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

前端 未结 3 1466
执念已碎
执念已碎 2020-12-19 15:31


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

相关标签:
3条回答
  • 2020-12-19 15:46

    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"))
    
    0 讨论(0)
  • 2020-12-19 15:58

    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}
    
    0 讨论(0)
  • 2020-12-19 16:05

    @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"));
    
    0 讨论(0)
提交回复
热议问题