Reputation: 697
I am building a website where users will be allowed to upload images . There is also restriction on maximum amount of space each user can use .
I have two ideas in mind .
Which among the above is better? and Why?
Upvotes: 8
Views: 2020
Reputation: 46301
sigh why does everybody jump to GridFS?
Depending on the size of the images and the exact use case, I'd recommend to store the images directly in the DB (not via GridFS). Here's why:
File System
af/2c/af2c2ab3852df91.jpg
, where the folders af
and 2c
are inferred from the file name (which itself might be a hash of the content for deduplication purposes).GridFS
GridFS is made for storing large files, and for storing files in a very similar way to a file system. That comes with some disadvantages:
fs.file
and one fs.chunk
document. Chunking is totally required for large files, but if your files are below 256k on average, there's no real chunking going on (default chunk size is 256k). So when storing small files in GridFS, you get the overhead without the advantage. Bad deal. It also requires two queries instead of one.Things might look different if you're operating a site for photographers where they can upload their RAW files or large JPEGs at 10MB. In that case, GridFS is probably a good choice. For storing user images, thumbnails, etc., I'd simply throw the image in its own document flat.
Upvotes: 14