Image files in MongoDB

Posted on 2014-08-25
Medium Priority
Last Modified: 2016-02-10
Hello Experts,

I need to store many images in the server.

     Is it good/best/correct to store  the image in the mongoDB? or to store the image on the disk, and storing the metadata of the image in the mongoDB.

Question by:ias1021
LVL 38

Expert Comment

by:Jim P.
ID: 40286504
I'm not sure with Mongo which is better.

Oracle and SQL you definitely want to go the metadata way.
LVL 46

Expert Comment

ID: 40286518
I'm a proponent of separate stores for media and the database.
LVL 49

Accepted Solution

PortletPaul earned 2000 total points
ID: 40287225
You will find arguments for both: Yes, use the db and No, avoid the db

Like most things "it will depend" on what those images are, how large they are, where they come from (e.g. are they user uploads?), how static they are and factors like how you want to do backups and recovery.

MongoDB is a "document oriented db" and very different to Oracle/MSSQL etc so it really shouldn't be judged the same way. Here are some arguments for using it:

When should I use GridFS?

For documents in a MongoDB collection, you should always use GridFS for storing files larger than 16 MB.

In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem.

•  If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.

•  When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities. When using geographically distributed replica sets MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.

•  When you want to access information from portions of large files without having to load whole files into memory, you can use GridFS to recall sections of files without reading the entire file into memory.

Do not use GridFS if you need to update the content of the entire file atomically. As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata. You can update the metadata field that indicates “latest” status in an atomic update after uploading the new version of the file, and later remove previous versions if needed.

Furthermore, if your files are all smaller the 16 MB BSON Document Size limit, consider storing the file manually within a single document. You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.

MongoDb's GridFS certainly has proponents for using it, e.g. Markus Gattol

Another big plus is that if we use the ordinary filesystem, we would have to handle backup/replication/scaling ourselves. We would also have to come up with some sort of hashing scheme ourselves plus we would need to take care about cleanup/sorting/moving because filesystems do not love lots of small files.

With GridFS, we can use MongoDB's built-in replication/backup/scaling e.g. scale reads by adding more read-only slaves and writes by using sharding. We also get out of the box hashing (read uuid (universally unique identifier)) for stored content plus we do not suffer from filesystem performance degradation because of a myriad of small files.

Also, we can easily access information from random sections of large files, another thing traditional tools working with data right off the filesystem are not good at. Last but not least, we can keep information associated with the file (who has edited it, download count, description, etc.) right with the file itself.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question