You can store images in Mongodb using Nodejs

I understand that Mongodb can store images in two ways.

  • in a regular document, saving the image as binary
  • via gridfs to manage large images.

For simplicity and because the images that I plan on the server are small, I will go to option 1.

For serving images in a browser, I use nodejs.

My question is: how difficult will it be? How do you turn binary data into an actual image that the browser will understand? What type of coding is involved?

Could you point me to tutorials / examples elsewhere on the Internet?

By the way, I know that this may not be very good for performance reasons, I plan to cache images after they are submitted. I just want to avoid the file system all together.

+5
source share
2 answers

I would strongly advise against serving images from MongoDB.

It would be better to save them on a static filet (S3) and possibly save the path in MongoDB.


You would probably use base64 encoding to put the file in mongodb: http://www.greywyvern.com/code/php/binary2base64/ (or just the base64 shell utility).

If you use ordinary documents, then the cost of execution is relatively low (as long as caching is good). If you use a mixed database, where you have GridFS and regular documents, you will need a lot of RAM on your server (s). GridFS queries will run completely differently than document queries.

:

var base64Data = imagefile.replace(/^data:image\/png;base64,/,""),
var dataBuffer = new Buffer(base64Data, 'base64');

// below line won't actually work but it something along the lines of what you want:

db.foo.insert({magic: 123, etc... img: dataBuffer.toString()})
+5

, , - , .

, , PNG, mimetype image/png, .

PNG

+2

All Articles