简体   繁体   English

Mongodb如何获取单个文档的大小?

[英]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:我找到了两种可能的解决方案:

  • Object.bsonsize - some javascript method that should return a size in bytes Object.bsonsize - 一些应该返回字节大小的 javascript 方法
  • db.collection.stats() - where there is a line 'avgObjSize' that produce some "aggregated"(average) size view on the data. db.collection.stats() - 有一行 'avgObjSize' 在数据上产生一些“聚合”(平均)大小视图。 It simply represents average size of single document.它只是表示单个文档的平均大小。

  • When I create test collection with only one document, both functions returns different values. 当我只用一个文档创建测试集合时,两个函数都返回不同的值。 How is it possible? 这怎么可能?
    Does it exist some other method to get a size of a mongo document? 是否存在其他方法来获取 mongo 文档的大小?

Here, I provide some code I perform testing on:在这里,我提供了一些我执行测试的代码:

  1. I created new database 'test' and input simple document with only one attribute: type:"auto"我创建了新的数据库“测试”并输入了只有一个属性的简单文档:类型:“自动”

     db.test.insert({type:"auto"})
  2. 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

    } }

  3. 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()函数。

With mongodb 4.4 (upcoming), You can use bsonSize operator to get the document size.使用 mongodb 4.4 (即将推出),您可以使用bsonSize运算符来获取文档大小。

db.test.aggregate([
  {
    "$project": {
      "name": 1,
      "object_size": { "$bsonSize": "$$ROOT" }
    }
  }
])

Object.bsonsize(db.test.findOne({type:"auto"})) 它以字节为单位给出。

Method Object.bsonsize() is available only in legacy mongo shell.方法Object.bsonsize()仅在旧版mongo shell 中可用。 In new mongosh you have to use package bson在新的mongosh中,您必须使用包bson

const BSON = require("bson");

BSON.calculateObjectSize({field: "value"})

BSON.calculateObjectSize(db.test.findOne())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM