简体   繁体   中英

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

We have a meteor / node app (wekan fork) and an underlying mongodb. To store Files we use cfs:gridfS .

I know that in the metadata in the files collection when uploading a document is storing the size of that document as length property (correct me if i am wrong).

So i was wondering is there also a way to get the complete size of all files without parsing (adding) them manually?

My Collections:

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

A 4kb size on disc document in the cfs_gridfs.attachments.files collection has two properties:

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

All Files in GridFS are stored in fs.files and fs.chunks , where a chunk has a default size of 255KB

Thus you may get the whole size in KB by calculating

fs.chunks.find().count() * 255

Note that this assumes, that you use the default db.fs as your GridFS bucket and the default chunksize.

In Meteor you can get the Grid Collections as every other collection:

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

Edit: this method is somewhat unprecise, because

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.

So you will get a result that is much larger than the real size, with increasing number of files. It won't be much of problem if you have a few, but large files.

However, other methods still require aggregate , which has already been described well , or a manual traversal of fs.files and summing up it's length property.

It's better to get the storage size for the collection using

db.fs.chunks.stats()

get the storage size from the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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