Get the file size in GridFS

1,551 views
Skip to first unread message

Jeet

unread,
Aug 24, 2012, 5:24:48 AM8/24/12
to mongod...@googlegroups.com
Hi all , i am storing the files using GridFS in mongo using spring .Now i want to get the size of all the files stored in mongo .So that i can restrict the user to a limit under 2 GB . Any solution is welcome .

Octavian Covalschi

unread,
Aug 24, 2012, 8:22:33 AM8/24/12
to mongod...@googlegroups.com
I think it gets to how to do a Map Reduce sum on a field, in this case is field "length"

In 2.2 it should be easier with aggregation framework, but before that it's a map reduce thing imho...

On Fri, Aug 24, 2012 at 4:24 AM, Jeet <kkhare...@gmail.com> wrote:
Hi all , i am storing the files using GridFS in mongo using spring .Now i want to get the size of all the files stored in mongo .So that i can restrict the user to a limit under 2 GB . Any solution is welcome .

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Sid

unread,
Aug 24, 2012, 1:02:07 PM8/24/12
to mongod...@googlegroups.com
GridFS contains the objects metadata in files collection.  Documents in the files collection have a field 'length'  that contains the size of the file in bytes. You can use that field to calculate the size of all the files stored in mongo.

Jason R. Coombs

unread,
May 11, 2016, 2:50:38 PM5/11/16
to mongodb-user
Here's what I did using the aggregation framework to find the size of all files in "dists/":

> db.fs.files.aggregate([
... {$match:
... {filename:
... {$regex: "dists/.*"}
... }
... },
... {$group:
... {
... _id: 0,
... total:
... {$sum: "$length"}
... }
... }
... ])
{ "_id" : 0, "total" : NumberLong("13415591553") }
Reply all
Reply to author
Forward
0 new messages