Mongo =获取单个文档的大小 [英] Mongo = get size of single document
问题描述
我遇到了一个奇怪的mongo行为,我想稍微澄清一下......
我的请求很简单:我想在集合中获得单个文档的大小。
我发现了两种可能的解决方案:
I encountered a strange behavior of mongo and I would like to clarify it a bit...
My request is simple as that: I would like to get a size of single document in collection.
I found two possible solutions:
- Object.bsonsize - 一些应该以字节为单位返回大小的javascript方法
- db.collection.stats() - 其中有一行avgObjSize,它在数据上产生一些聚合(平均)大小视图。它只代表单个文档的平均大小。
当我用一个文档创建测试集合时,两个函数都返回不同的值。这怎么可能?
是否存在其他方法来获取mongo文档的大小?
在这里,我提供了一些代码我执行测试:
Here, I provide some code I perform testing on:
-
我创建了新的数据库'test'并输入只有一个属性的简单文档:type: auto
I created new database 'test' and input simple document with only one attribute: type:"auto"
db.test.insert({type:"auto"})
从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
}
从bsonsize函数调用输出: Object.bsonsize(db.test.find({test:auto }))
output from bsonsize function call: Object.bsonsize(db.test.find({test:"auto"}))
481
推荐答案
我找到了一个解决方案。在之前调用的 Object.bsonsize 中,mongo返回了CURSOR的大小而不是文档本身。
I have found a solution. In the previous call of Object.bsonsize mongo returned size OF THE CURSOR rather than the document itself.
正确的方法是使用此命令:
Correct way is to use this command:
Object.bsonsize(db.test.findOne({type:"auto"}))
这将返回正确的特定文档的大小(以字节为单位)。
this will return the correct size of the particular document (in bytes).
这篇关于Mongo =获取单个文档的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!