GridFS Collection:如何获取所有上传文件的总大小?

GridFS Collection: howto get total size of all uploaded Files?

我们有一个流星/节点应用程序(wekan 分支)和一个底层 mongodb。 我们使用 cfs:gridfS.

来存储文件

我知道在文件 collection 的元数据中上传文档时会将该文档的大小存储为长度 属性(如果我错了请纠正我)。

所以我想知道是否还有一种方法可以在不手动解析(添加)文件的情况下获取所有文件的完整大小?

我的Collections:

cfs._tempstore.chunks
cfs.attachments.filerecord
cfs_gridfs._tempstore.chunks
cfs_gridfs._tempstore.files
cfs_gridfs.attachments.chunks
cfs_gridfs.attachments.files

cfs_gridfs.attachments.files collection 中的一个 4kb 大小的光盘文件有两个属性:

length: 84 // is correct the size of the file but not what it's used on disc
chunksize: 2097152 // don't understand this number, way to much for a 4kb document

GridFS 中的所有文件都存储在 fs.filesfs.chunks 中,其中 chunk has a default size of 255KB

因此您可以通过计算

得到以 KB 为单位的整个大小
fs.chunks.find().count() * 255

请注意,这假定您使用默认 db.fs 作为您的 GridFS 存储桶和默认块大小。

在 Meteor 中,您可以获得与所有其他集合一样的网格集合:

const FsChunks = new Mongo.Collection('fs.chunks')

编辑:这个方法有些不精确,因为

The last chunk is only as large as necessary. Similarly, files that are no larger than the chunk size only have a final chunk, using only as much space as needed plus some additional metadata.

所以你会得到一个比实际大小大得多的结果,文件数量越来越多。如果您有一些大文件,问题不大。

但是其他方法还是需要aggregate,已经是described well,或者手动遍历fs.files,总结起来就是length属性.

最好使用

获取集合的存储大小
db.fs.chunks.stats()

从输出中获取存储大小。