Store files in mongodb with Nodejs
It seems that you are trying to save a really big amount of information with mongoDb.
I can think in 3 diferent options for your case
Cloud Services
- As other people already comment here, if the file that you are saving is a compressed one, even if its a small file, the new compression wont help you. In this cases, my recomendation is to use some web cloud service that is already optimized for the kind of information that you are trying to save and retrive, if its an image you could use Cloudinary that also has a free service so you can test it.
Local Storage and saving routes in DB
- Other solution could be storing the encoded data in a .txt file, storing it in a cloud or in your file sistem, and then only save the routing in the database. This way you will not depend on the mongoDB speed for retriving it but you will have a good way to know where the files are located.
Using MongoDB and GridFS
- This way you can use a specific method to store information in MongoDB that is recomended when you are dealing with files that are 16mb. As the Official Documentation says:
Instead of storing a file in a single document, GridFS divides the file into parts, or chunks [1], 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.
And next they say in what situations you may use this way of storing information:
In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem.
- If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.
- When you want to access information from portions of large files without having to load whole files into memory, you can use GridFS to recall sections of files without reading the entire file into memory.
- When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities, you can use GridFS. When using geographically distributed replica sets, MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.
Hope it was useful :)