Image files in MongoDB

Posted on 2014-08-25
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.
    I'm not sure with Mongo which is better.

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

    Expert Comment

    I'm a proponent of separate stores for media and the database.
    LVL 47

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    With a software solution, you always get to control email signatures and have the final say on the design - not an unrelated department or the uninterested end user.
    An overview of how to create reports in Adobe Analytics (formerly Omniture Site Catalyst) using pageNames, events, eVars and props. This video will show you how to install the Omniture Debugger tool so can see (and test) what is being passed int…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now