Storing images in MongoDB

hello community! i am new to MongoDB, i wanna know if mongodb is good for storing images and how i can store images in mongodb. I am using Node, Express framework and reactjs. Pls i need a perfect tutorial on how i can work with images. Thanks a bunch.

1 Like

Look at https://docs.mongodb.com/manual/core/gridfs/

1 Like

No, MongoDB is not a good place for storing files. If you want to store files, you should use storages like Amazon S3 or Google Could Storage.

The good practice is to store the files in a storage and then to just save the URL of the uploaded image in the MongoDB.

When it comes to Node.js and Express, I would suggest multer package middleware, which is primarily used for uploading files. If you would use Amazon S3 for a storage, there is also a plug-in multer-s3 package that can be helpful.

You can check this video on how to configure Multer and implement image upload on your Node.js server. Once the image is uploaded, Multer will return data to you with destination field included. You can then save that destination in the MongoDB.

10 Likes

i am very grateful. I hope it solves my problem.

1 Like

I had a task last week that required me to upload images to a database. I’ve S3 buckets to store images in the past but I tried MongoDB this time. So, I used a package called express-fileuploader, very similar to Multer, and uploaded my file to a temp directory in the server, from there I grabbed it using the native fs methods and uploaded it to the DB. The image file was uploaded as binary data, ultimately when getting the data you get it as a buffer base 64 if I’m not mistaken. JS can render using different methods.

In conclusion, use an S3 bucket or google cloud storage. Uploading to MongoDB is not as complicated as it sounds, but you’ll definitely save some time using other services.

1 Like

Wish people could be much of a use like you

can u explain why mongo is not a good place to store files does that include image files ?

hey can u provide some code to do so i am new to node and mongo