使用以下命令,我可以获取特定集合的数据大小:
db.collection.dataSize()
如何获取数据库的数据大小?
在Mongo shell中使用以下命令:
db.stats()
输出应该像这样:
{
"collections" : 3,
"objects" : 80614,
"dataSize" : 21069700,
"storageSize" : 39845376,
"numExtents" : 9,
"indexes" : 2,
"indexSize" : 6012928,
"ok" : 1
}
查看更多诊断命令,请点击此处。
在Java中使用db.getStats()
CommandResult re = db.getStats();
for(String k: re.keySet()){
System.out.println(k+"="+re.get(k) );
}
serverUsed=127.0.0.1:27017
db=test
collections=3
objects=100004
avgObjSize=67.99876004959802
dataSize=6800148
storageSize=10633216
numExtents=9
indexes=1
indexSize=4406864
fileSize=50331648
nsSizeMB=16
ok=1.0
db.stats() //For db status
db.<collectionname>.stats() //For collection status
在Python中:
connection = pymongo.Connection(host = "127.0.0.1", port = 27017)
db = connection["test_db"]
test_collection = db["test_collection"]
db.command("dbstats") # prints database stats for "test_db"
db.command("collstats", "test_collection") # prints collection-level stats for "test_collection" under "test_db".
$con= new Mongo()
$stats=$con->dbName->command(array('dbStats' => 1)); // for db.stats()
$stats=$con->dbName->command(array('collStats' => 'collection_name')); // for db.collection_name.stats()