14

I was wondering which is better/faster:

  1. Having a separate collection of documents that just contain the image saved as binary data, and possibly some metadata.
  2. Or using GridFS to store the images.
mcls
  • 9,071
  • 2
  • 29
  • 28

3 Answers3

23

If your images are small you can store them as binary data in the documents in your collection. Just consider that you will be retrieving them every time you query your document (unless you exclude the 'image' field from your queries).

However, if your images are larger I would use GridFS. GridFS has some features that make it very good at handling images that you should consider:

  • For larger images, when they are stored in GridFs they will be split in chunks and you can store very large files. If you try to store images in your document, you are constrained by the 16Mb max size of a document, and you are consuming space that needs to be used for your actual document.
  • You can add metadata to the image itself and run queries against these attributes, as if you were doing it from a regular document in a collection. So GridFS is as good as a document for metadata about the image.
  • I really like that I get MD5 hash calculated on the images. (It is very useful for some of my cases).
  • By storing images in GridFS you save yourself the preprocessing of the image into binary format (not a big deal, but a convenience of GridFS)

In terms of performance, reading/writing against a regular document should be no different than doing it against GridFS. I would not consider performance to be a differentiator in choosing either one.

My personal recommendation is to go with GridFS, but you need to analyze for your particular use case.

Hope this helps.

agarcian
  • 3,909
  • 3
  • 33
  • 55
3

I use GridFS to store photos and documents. It's so easy and retrieving it from the collection to display or save locally is easy. You can store metadata along w/ the binary data inside the same collection. This way you don't need to create an additional collection to store them.

For example, in one of my project I store user profile photos along with usernames, file type, and date of upload.

sdot257
  • 10,046
  • 26
  • 88
  • 122
  • helo @luckytaxi ,"0 down vote I use GridFS to store photos and documents. It's so easy and retrieving it from the collection to display or save locally is easy." can u explain how u do image uploading , retrieving and displaying locally? – Pravitha V Sep 29 '12 at 09:05
  • 2
    Any examples of this by any chance, I am actually trying to do this exactly for user profile pictures – Lion789 Aug 20 '13 at 04:51
1

GridFS is developed to handle Files in an efficient way. http://www.mongodb.org/display/DOCS/When+to+use+GridFS

Do not forget that you maybe will have to translate the data to a file and back.

But to be sure, do a performance test that takes account of your usage pattern.

hellectronic
  • 1,390
  • 11
  • 15