shix
shix

Reputation: 77

How to calculate the data size of the Mongodb collection using Java MongoOperations?

Mongodb has a mongo shell command db.getCollection('myCollection').dataSize() to return the data size of the given collection. However, this does not work with MongoOperations getCollections() in Spring.

To work around this is what I tried :

myMongoOpObject.executeCommand("db.getCollection(collectionName).dataSize()");

However, this does not work. No errors or console prints. Any help will be appreciated.

Upvotes: 1

Views: 1897

Answers (2)

iska
iska

Reputation: 2248

You need the collStats command, see here: https://docs.mongodb.com/manual/reference/command/collStats/

Document stats = operations.executeCommand(new Document("collStats", "collectionName"));
System.out.println(stats.get("count"));
System.out.println(stats.get("avgObjSize"))
System.out.println(stats.get("storageSize"))

Upvotes: 2

vinay jain
vinay jain

Reputation: 93

Use below syntax

 List<MyCollection> list = mongoOperation.findAll(MyCollection.class);
 System.out.println("Number of records = " + list.size());

Upvotes: -1

Related Questions