Mongodb – storing files on mongodb

awsmongodb

I am developing an app where user will upload multiple images along with some other data like name address etc.

I am using MEAN stack for development, So I have some questions regarding this:

  1. How should I store image data in my db, like what information should I save in my db ?

  2. I have read that we can use GridFS by mongodb. But my images would be of max 5MB in size. So what should I use simple fs by node or gridfs by mongodb ?

  3. What is the need of storing file data on amazon s3, can't I just save base64 string into my database ?

I have googled more and found out that storing images on storage is more beneficial than storing them directly into db. So I think I will go with s3, but then how can I refer to the images stored in s3 from the db ?

my app consist of 200 users and each user can upload upto 5 images and upto 5mb of size, and yes it is performance critical also.

Best Answer

I've worked on a project where all files where stored inside the database (SQL Server). The decision was made because the system used 2 machines with Mirroring for High Availability and saving the files to the file disk would be troublesome to handle.

The engineer that decided this at the time didn't know how easy, fast and cheap is to store files in the Cloud and we end up with a 11 GB database where 10 GB consists only of files. It was expensive to save backups and the db restore process (for testing or disaster recovery) was much slower than needed.

Using MongoDB GridFS is quite easy and you can start by following the documentation here or with this tutorial, but I would not recommend it. There are many discussions of pros and cons of using the DB to store files and you may agree with me after reading these links:

The two major pros are:

  • Performance (that is critical for your use case)
  • Cost (DB storage is much more expensive than Cloud storage)

If you store the image in S3, you will end up with a link similar to this:
https://s3-sa-east-1.amazonaws.com/mybucketname/myimage.png

This is a very lightweight info that you can store in your Mongo document. To retrieve this file, you have two common options:

  • If its a file URL that's open to the world, you can give the URL to the client browser and make the Angular app to download it. If you use Amazon CloudFront (CDN service), the low latency server will be a real boost for your user.
  • If the file is private, you can use your Node.js app to download it and send a copy to your user.