[英]How to get the size of single document in Mongodb?
I encountered a strange behavior of mongo and I would like to clarify it a bit...我遇到了mongo的一个奇怪行为,我想澄清一下......
My request is simple as that: I would like to get a size of single document in collection.我的要求很简单:我想在集合中获取单个文档的大小。 I found two possible solutions:
我找到了两种可能的解决方案:
Here, I provide some code I perform testing on:在这里,我提供了一些我执行测试的代码:
I created new database 'test' and input simple document with only one attribute: type:"auto"我创建了新的数据库“测试”并输入了只有一个属性的简单文档:类型:“自动”
db.test.insert({type:"auto"})
output from stats() function call: db.test.stats() : stats() 函数调用的输出: db.test.stats() :
{ "ns" : "test.test", "count" : 1, "size" : 40, "avgObjSize" : 40, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 4096, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 0, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1
} }
output from bsonsize function call: Object.bsonsize(db.test.find({test:"auto"})) bsonsize 函数调用的输出: Object.bsonsize(db.test.find({test:"auto"}))
481
In the previous call of Object.bsonsize()
, Mongodb returned the size of the cursor, rather than the document.在之前的
Object.bsonsize()
调用中,Mongodb 返回的是游标的大小,而不是文档。
Correct way is to use this command:正确的方法是使用这个命令:
Object.bsonsize(db.test.findOne())
With findOne()
, you can define your query for a specific document:使用
findOne()
,您可以定义对特定文档的查询:
Object.bsonsize(db.test.findOne({type:"auto"}))
This will return the correct size (in bytes) of the particular document.这将返回特定文档的正确大小(以字节为单位)。
I recommended to use this script to get the real size.我建议使用此脚本来获取实际大小。
db.users.find().forEach(function(obj)
{
var size = Object.bsonsize(obj);
print('_id: '+obj._id+' || Size: '+size+'B -> '+Math.round(size/(1024))+'KB -> '+Math.round(size/(1024*1024))+'MB (max 16MB)');
});
Note: If your IDs are 64-bit integers, the above will truncate the ID value on printing!注意:如果您的 ID 是 64 位整数,则上述内容将在打印时截断 ID 值! If that's the case, you can use instead:
如果是这种情况,您可以改用:
db.users.find().forEach(function(obj)
{
var size = Object.bsonsize(obj);
var stats =
{
'_id': obj._id,
'bytes': size,
'KB': Math.round(size/(1024)),
'MB': Math.round(size/(1024*1024))
};
print(stats);
});
This also has the advantage of returning JSON, so a GUI like RoboMongo can tabulate it!这也有返回 JSON 的优势,所以像 RoboMongo 这样的 GUI 可以将它制表!
source : https://stackoverflow.com/a/16957505/3933634来源: https : //stackoverflow.com/a/16957505/3933634
edit : thanks to @zAlbee for your suggest completion.编辑:感谢@zAlbee的建议完成。
The effective amount of space the document will take in the collection will be more than the size of your document because of the Record Padding mechanism.由于记录填充机制,文档在集合中占用的有效空间量将大于文档的大小。
This is why there is a difference between the outputs of the db.test.stats()
and Object.bsonsize(..)
.这就是
db.test.stats()
和Object.bsonsize(..)
的输出之间存在差异的原因。
To get the exact size (in bytes) of the document, stick to the Object.bsonsize()
function.要获得文档的确切大小(以字节为单位),请坚持使用
Object.bsonsize()
函数。
Object.bsonsize(db.test.findOne({type:"auto"})) 它以字节为单位给出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.