Storing data (images / audio / video) > 16 MB in MongoDB or GridFS?

Hi,
I have a requirement to store data in MongoDB (Images / audio / video), the file size could be > 16 MB.
I have gone through the documentation which suggests :
If data size < 16 MB
store in MongoDB document
else
store in GridFS

I have the following queries:
Q1) Is it advisable to store data > 16 MB in multiple documents in MongoDB as per required “chunk size”, do custom logic to split the files as per “chunk size” & query the chunks for a given (images / audio / video) file from MongoDB or use GridFS for this ? Which is a better option & why ?

Q2) If using GridFS, how do I decide for the chunk size for files of varying sizes ( > 16 MB) to get best performance while quering.

Thanks,
Sachin Vyas

Hello :wave: @Sachin_Vyas,

Welcome to the MongoDB Community forum :sparkles:

Based on your requirements, it is recommended to use GridFS to store data in MongoDB for files larger than 16 MB. Storing data larger than 16 MB in multiple documents in MongoDB is generally not advisable as it can cause performance issues and increase the risk of data inconsistencies.

GridFS is designed to handle large files by storing them as separate documents with a fixed chunk size. The default chunk size in GridFS is 255 KB, but you can specify a different size when creating the GridFS bucket. The optimal chunk size may depend on the type and size of the files you are storing, as well as the resources of your hardware and system.

Using GridFS also allows you to take advantage of MongoDB’s powerful querying capabilities to retrieve files efficiently.

Although officially supported, GridFS is a convention rather than a specific built-in method, thus the binary files are stored as regular MongoDB documents. In most cases it’s preferable to store binaries outside the database, where the database entry contains the pointer toward the relevant file. However if storing binaries in the database is desired or required for the use case, then GridFS is a valid solution.

I hope this helps answer your questions. Let me know if you have any further queries.

Best,
Kushagra