database mongodb 中的数据库数据大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6178128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:13:07  来源:igfitidea点击:

database dataSize in mongodb

mongodbdatabase

提问by JuanPablo

With following command, I can get the data size of a specific collection:

使用以下命令,我可以获得特定集合的数据大小:

db.collection.dataSize()

How do I get the datasize of a database?

如何获取数据库的数据大小?

回答by Andrew Orsich

Use following command at mongoshell:

在 mongoshell 中使用以下命令:

 db.stats()

Output should be like this:

输出应该是这样的:

{
  "collections" : 3,
  "objects" : 80614,
  "dataSize" : 21069700,
  "storageSize" : 39845376,
  "numExtents" : 9,
  "indexes" : 2,
  "indexSize" : 6012928,
  "ok" : 1
}

See more diagnostic command here.

在此处查看更多诊断命令。

回答by Anderson Mao

Use db.getStats() in Java

在 Java 中使用 db.getStats()

CommandResult re = db.getStats();
for(String k: re.keySet()){
    System.out.println(k+"="+re.get(k) );
}

Then will get result like below:

然后会得到如下结果:

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

回答by J.M.

The dbStatscommand returns different storage statistics for a given database. As explained in this postabout the different MongoDB performance metrics you should monitor (with MMAPv1), the dataSizemetric that you mentioned measures the space taken by all the documents and padding in the database. To get the "storage footprint" of you database on disk, you should look at the fileSizemetrics which corresponds to the size of your data files. It decreases only if you delete a database and is not affected when collections, documents, or indexes are removed.

dbStats命令返回给定的数据库不同的存储统计数据。正如这篇关于您应该监控的不同 MongoDB 性能指标(使用 MMAPv1)的文章中所述dataSize,您提到的指标衡量数据库中所有文档和填充占用的空间。要在磁盘上获取数据库的“存储空间”,您应该查看fileSize与数据文件大小相对应的指标。它仅在您删除数据库时减少,并且在删除集合、文档或索引时不受影响。

Here is a diagram with the different important storage metrics returned by dbStats: enter image description here

以下是 dbStats 返回的不同重要存储指标的图表: 在此处输入图片说明

Also note that, with the MMAPv1 storage engine, the mapped memory (mem.mappedmetric), corresponding to the quantity of virtual memory used to map the database into memory, gives you a good approximation of the total size of your database(s). You can access this metric with the serverStatus command.

另请注意,对于 MMAPv1 存储引擎,映射内存(mem.mapped度量)与用于将数据库映射到内存的虚拟内存量相对应,可以很好地近似估计数据库的总大小。您可以使用serverStatus 命令访问此指标。

回答by Amitesh

In MongoDB :

在 MongoDB 中:

db.stats()         //For db status
db.<collectionname>.stats() //For collection status

In Python :

在 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".  

In PHP :

在 PHP 中:

$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()