Store images in a MongoDB database Store images in a MongoDB database database database

Store images in a MongoDB database


Please see the GridFS docs for details on storing such binary data.

Support for your specific language should be linked to at the bottom of the screen.


"You should always use GridFS for storing files larger than 16MB" - When should I use GridFS?

MongoDB BSON documents are capped at 16 MB. So if the total size of your array of files is less than that, you may store them directly in your document using the BinData data type.

Videos, images, PDFs, spreadsheets, etc. - it doesn't matter, they are all treated the same. It's up to your application to return an appropriate content type header to display them.

Check out the GridFS documentation for more details.


You can try this one:

String newFileName = "my-image";File imageFile = new File("/users/victor/images/image.png");GridFS gfsPhoto = new GridFS(db, "photo");GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);gfsFile.setFilename(newFileName);gfsFile.save();