java查看mongodb集合表空间大小

话不多说先粘代码:

MongoDatabase db = mongoTemplate.getDb();
Document collStats = db.runCommand(new Document("collStats", "collectionname"));
int size = (int) collStats.get("size");

mongo中有xshell命令 

db.runCommand({collStats : 'player',scale : 1024}) 如:

db.runCommand({collStats : 'player',scale : 1024})
{
    "ns" : "zhanguo_server_jd01.player",    
    "count" : 93263,                        
    "size" : 94040,
    "avgObjSize" : 1.008331278213225,
    "storageSize" : 106988,
    "numExtents" : 8,
    "nindexes" : 4,
    "lastExtentSize" : 37796,
    "paddingFactor" : 1.000000000001303,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 20607,
    "indexSizes" : {
        "_id_" : 3113,
        "uid_1" : 8543,
        "user_name_1" : 4032,
        "email_1" : 4918
    },
    "ok" : 1
}
启发链接1:https://blog.csdn.net/mnasd/article/details/80110700;
启发链接2:https://bbs.csdn.net/topics/392509506;
注意事项:无论是返回结果还是查询方法中的参数都要用org.bson.Document封装.
记录日期:2019-08-07

你可能感兴趣的:(mongo中间件使用)