7

Is there a way to get the size of all the documents that meets a certain query in the MongoDB shell?

I'm creating a tool that will use mongodump (see here) with the query option to dump specific data on an external media device. However, I would like to see if all the documents will fit in the external media device before starting the dump. That's why I would like to get the size of all the documents that meet the query.

I am aware of the Object.bsonsize method described here, but it seems that it only returns the size of one document.

4

2 回答 2

14

这是我找到的答案:

var cursor = db.collection.find(...); //Add your query here.
var size = 0;
cursor.forEach(
    function(doc){
        size += Object.bsonsize(doc)
    }
);
print(size);

应该非常准确地输出文档的字节大小。

我已经运行了两次命令。第一次,有 141 215 份文件,一旦被转储,总大小约为 108 mb。该命令的输出与磁盘大小之间的差异为 787 字节。

我第二次运行该命令时,有 35 914 179 个文档,一旦转储,总共有大约 57.8 GB。这一次,我在命令和磁盘上的实际大小之间具有完全相同的大小。

于 2015-11-19T14:45:05.793 回答
1

从 开始Mongo 4.4$bsonSize在编码为 BSON 时返回给定文档的大小(以字节为单位)。

因此,为了总结与您的查询匹配的所有文档的 bson 大小:

// { d: [1, 2, 3, 4, 5] }
// { a: 1, b: "hello" }
// { c: 1000, a: "world" }
db.collection.aggregate([
  { $group: {
    _id: null,
    size: { $sum: { $bsonSize: "$$ROOT" } }
  }}
])
// { "_id" : null, "size" : 177 }

$group是所有匹配的项目,并且$sum是分组文档$bsonSize

$$ROOT表示我们从中获取 bsonsize 的当前文档。

于 2021-12-04T11:25:45.633 回答