Process of storing images in MongoDB

Hello everyone, I`m new to using MongoDB and I need to write an essay about the storage of images in MongoDB. Do you know where I can find ressources that explain the theory of how images are stored in MongoDB? Like explaining the process step by step and how they are retrieved? I’d be thankful for every piece of information I can get.
Thank you!

Please go through the documentation for more details.


If you need to store the actual file on MongoDB, then GridFS is the way to go (as suggested by @Sudhesh_Gnanasekaran). However there is an alternative – store a url to an image in your document.

For example, I have a business collection which has a field, logoUrl. This is a url to an actual cloud storage solution such as Amazon S3. It could also be a url to a CDN like Cloudflare or Fastly.

MongoDB used to have an application called Stitch that made S3 integration easy. It transitioned into Realm which may have a different process.

Otherwise, the manual process is straight forward:
A frontend/client-facing application allows a user to upload a file, the file is sent via an API to a storage solution (AWS S3, Google Cloud Storage, Backblaze, etc), the URL response from the upload is sent back and the record in the database is updated/created with the URL to the image.


Hello @Maria_N_A, welcome to the MongoDB Community forum.

In addition to GridFS and image locations within the document, you can store small image data (like profile photos) within a document itself. MongoDB has a data type called as “Binary data” (see MongoDB BSON Types). Note that a MongoDB document can be up to 16 Megabytes.


Welcome to the MongoDB Community @Maria_N_A!

Per the earlier suggestions, there are three common approaches for working with images and other binary assets:

  • GridFS: As suggested by @Sudhesh_Gnanasekaran, large images (or binary blobs) can be stored using the GridFS API. This API is supported by official MongoDB drivers: it splits large files into smaller chunks (255KiB by default) which are stored as separate documents in an fs.chunks collection with a reference document including metadata in an fs.files collection (note: the default fs.* namespace can be changed). The GridFS API is a client-side implementation – a MongoDB deployment doesn’t have any special configuration for the underlying collection data. For more info on the implementation, see the GridFS spec on GitHub.

  • Inline: As suggested by @Prasad_Saya, smaller images (within the 16MB document size limit) can be stored directly in a MongoDB document using the BinData (binary data) BSON type.

  • Reference: As suggested by @Andrew_W, images can be saved to an API or filesystem, with only the image reference stored in the database.

Storing binary files in a database can be convenient for distributing across multiple locations (via replication), for working around file system limitations (eg files per directory or file naming), for serving streaming or protected content, or for storing larger assets that aren’t going to be served directly to end users. Aside from the GridFS documentation page that has already been linked in an earlier comment, Building MongoDB Applications with Binary Files using GridFS (part 1 and part 2) may also be helpful reading.

If images or large binary assets are being served directly to end users, the Reference approach is usually most suitable because files can be pushed out to an API and/or CDN (Content Delivery Network) and cached/resized for better user experience. There is less overhead serving images directly from a web server versus going through an application server and database server for every request. A downside of using references is that they can get out of sync with the source document.

There are also hybrid use cases, such as storing large images (for example, raw images from a digital camera or phone) in the database and then passing those to an API or image processing library to create resized versions which will be served directly to end users.

Before deciding to store images in your database, I would make sure there is a clear benefit for the intended use case.



Is there a guide or tutorial to help with setting up referencing using S3. I am not using the command line. I am entirely using the MongoDB realm and atlas. How would I go about setting up the S3 with Mongo, reference the URL in a document?