9

I'm using mongodb and I want to store some thumbnails in my server. What's best? Using GridFS or converting those images to base64 and store them directly inside a document.

danielrvt
  • 10,177
  • 20
  • 80
  • 121
  • What's the final size in K? GridFs doesn't split until file is over 256k. Will you be using the final file as binary or base64? – WiredPrairie Mar 25 '13 at 11:05

3 Answers3

8

As always there are some (dis) advantages:

Pros:

  • Less Database requests if only the document+thumbnail is needed.
  • Less client requests. (of course you could fetch the thumbnails from GridFS, and put them within the response, but that would result in more database requests)

Neutral:

  • Storage requirements are equal

Cons:

  • You can't reuse the very same image thumbnail in another document easily, because there's no id to reference to. (For us, that's not an issue, because the server responses are gzip compressed and you can't really tell the difference between 1 and 5 equal images)

With MongoDB and NoSQL it's all about knowing your use cases!

  • If lot's of your documents share the same image, you should use GridFS and just provide links to those files, because 1. sharing data is more space efficient and 2. the client can cache the image request and just has to retrieve it once.

  • If your clients will always need the thumbnail, you maybe should consider embedding the files as base64 within the response. This is especially nice, if 1. images are not shared between documents and/or 2. images change often and caching is useless / not possible.

  • Base64 of course means more traffic on the wire, because it needs 8 bits to transfer 6 bits. i.e. 75% efficiency. This of course only affects the client-server communication, because within MongoDB you can always store your data as binary field.

  • Do you prefer more database requests (= using GridFS)? Or bigger data/document size on the wire (= embedded)?

What we did:

We use embedded thumbnails, even if we potentially have duplicate images. After activating gzip compression on the server, the server-client transfer size didn't matter anymore. But as said before, it's a tradeoff: Now we have less client requests and less database requests, but because embedding makes caching the images impossible, we now have more data on the wire.

Conclusion:

There's no one size fits all solution.

Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • Can you give an example on how are you get the link/url /path of your file after upload to GRIDFS(it could help a lot) ? – Danford Kija Mar 31 '20 at 10:15
  • When you upload a file to GridFS, MongoDB will generate an ObjectId - just like for any other document you store. GridFS isn't really a MongoDB feature, it's usually a part of your MongoDB Client library. For example: https://mongodb.github.io/mongo-java-driver/3.5/driver/tutorials/gridfs/ – Benjamin M Apr 05 '20 at 01:48
3

It really depends on your server side technology and personal preference. 10gen suggests you use documents unless you are storing files larger than the document limit (16MB). I would suggest that you do whatever is easier given the language you are working with. If you have other documents to model after follow the document, otherwise give gridFS a shot.

Charlie Strawn
  • 387
  • 2
  • 8
0

I suggest you to use GridFS. With GridFS, you can take the advantage of MongoDB REST API. So there won't be any overheat for retrieving documents using MongoDB API. REST API will do the all of hard work and will save you time.

ramazan polat
  • 7,111
  • 1
  • 48
  • 76