Nimit Bedi
Nimit Bedi

Reputation: 311

How can I add images in mongoDB?

I need to add images to my mongoDB using Node and Express. I am able to normal data in it by running the mongo shell. But I cannot find any method to add images to it. Can anybody help?

Upvotes: 4

Views: 1737

Answers (1)

rdegges
rdegges

Reputation: 33824

Please don't do this. Databases are not particularly well suited to storing large bits of data like images, files, etc.

Instead: you should store your images in a dedicated static file store like Amazon S3, then store a LINK to that image in your MongoDB record.

This is a lot better in terms of general performance and function because:

  • It will reduce your database hosting costs (it is cheaper to store large files in S3 or other file services than in a database).
  • It will improve database query performance: DBs are fast at querying small pieces of data, but bad at returning large volumes of data (like files).
  • It will make your site or application much faster: instead of needing to query the DB for your image when you need it, you can simply output the image link and it will be rendered immediately.

Overall: it is a much better / safer / faster strategy.

Upvotes: 6

Related Questions