Feb 26, 2019 by Thibault Debatty - 998 views
MongoDB is a fantastic tool for storing large quantities of data in a JSON-like format.
Furthermore, it can be used to store binary data with GridFS. This has multiple advantages:
Technically, GridFS is used to store and retrieve files that exceed the bson-document size limit of 16 MB. Instead of storing the file in a single document, GridFS divides the file into chunks and stores each chunk as a separate document.
To store a file in GridFS using Java:
//Bucket to generate GridFSUploadStreams GridFSBucket bucket = GridFSBuckets.create( mongo_client.getDatabase(db_name), bucket_name); //Each file needs a different uploadStream GridFSUploadStream uploadStream = bucket.openUploadStream(filename); byte data = ...; uploadStream.write(data) ; ObjectId fileid = uploadStream.getObjectId() ; uploadStream.close() ;
The file can now be retrieved using its fileid or filename :
FileOutputStream streamToDownloadTo = new FileOutputStream(local_file); //Using fileid bucket.downloadToStream(fileid, streamToDownloadTo) ; //Using mongo filename bucket.downloadToStream(filename, streamToDownloadTo); streamToDownloadTo.close() ;
You can also retrieve the data as a byte :
GridFSDownloadStream downloadStream = bucket.openDownloadStream(fileid); int fileLength = (int) downloadStream.getGridFSFile().getLength(); byte data = new byte[fileLength]; downloadStream.read(bytesToWriteTo); downloadStream.close();
Finally, you can use Mongo shell to check and retrieve your files: