Reputation: 531
i'm trying to store images in MongoDB, and I'm not sure how to go about doing this.
We have a bunch of different entries in our server and each of them have a different background image. Are we able to store the images on the server, or server folder, or should we put the images on like imgur and put the link in the database? I guess what im asking is how can i "upload" images to a mongoDB server?
Upvotes: 1
Views: 9545
Reputation: 307
You have few options but I would recommend 2 of them. Saving images as @Luca advised, in base64 is good but doesn't work on big files and images.
In MongoDB, use GridFS for storing files larger than 16 MB. Instead of storing a file in a single document, GridFS divides the file into parts, or chunks, and stores each chunk as a separate document. By default, GridFS uses a default chunk size of 255 kB; that is, GridFS divides a file into chunks of 255 kB with the exception of the last chunk. The last chunk is only as large as necessary. Similarly, files that are no larger than the chunk size only have a final chunk, using only as much space as needed plus some additional metadata.
With multer, you upload files to server and save routes to your database. When you are in need for those images, you take route/name of file and get it from server.
Upvotes: 1