Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use GridFS or binary data to store & retrieve images from MongoDB?

Tags:

mongodb

gridfs

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.
like image 449
mcls Avatar asked Oct 18 '11 11:10

mcls


People also ask

Can we store binary data in MongoDB?

In MongoDB, you can use the BSON binary type to store any kind of binary data. This data type corresponds to the RDBMS BLOB (binary large object) type, and it's the basis for two flavors of binary object storage provided by MongoDB. The first uses one document per file and is best for smaller binary objects.

How do you store binary data?

Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.

What is GridFS storage?

GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has the capability to store files even greater than its document size limit of 16MB.

Which of the following datatypes can hold large binary files in a database?

A CLOB (Character Large Object) is a data type that can be used to store a large collection of character data in a database table. For example, a digital file containing a picture, video, or a song can be stored in a database using a BLOB, or a plain text file can be stored in a database using a CLOB.


2 Answers

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.

like image 157
agarcian Avatar answered Nov 04 '22 02:11

agarcian


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.

like image 23
sdot257 Avatar answered Nov 04 '22 04:11

sdot257