How to store images in MongoDB

In this MongoDB tutorial, we will discuss and learn more about “How to store images in MongoDB database“. We will also learn how different ways we can store the image and easily retrieve the images from the MongoDB database. These are the following topic that we are going to cover in this tutorial

  • How to store images in MongoDB?
  • Storing images in MongoDB using python

How to store images in MongoDB?

We can store the images in the MongoDB database by creating a schema with mongoose. We define the schema by creating a file “model.js“.

To store the images in the database form of arrays, there is the data type that is Buffer which is used to store the image.

There are three approaches for working with images:

  • GridFS: Using GridFS API we can store the large images. The API will help you store the large file into small chunks (255KiB) and store it into separate documents in an “fs.chunks collection.
  • Inline: In this smaller images(16MB) can be stored into MongoDB documents using binary data.
  • Reference: In this, only image references are stored in the database and we can also save images into API or file system.

Storing the binary files in a database is convenient for distributing across multiple locations because large files are hard to access to end-users.

Note that, Before storing images in the database you need to make sure what is the benefit behind it.

Also Read: MongoDB group by count

Storing images in MongoDB using python

In this topic, we will learn to store images in MongoDB via python.

We will use two libraries for this:

  1. GridFS
  2. PyMongo

GridFS: It is used for storing and retrieving giant files like images, audios, videos, etc. In this, we have a tendency to store information in MongoDB collection. It’s the potential to store files even bigger than the scale limit of 16MB.

PyMongo: The PyMongo library is used to interacting with the MongoDB database from python. We perform various functionality operations like retrieve results, write and delete data, run the database.

Remember that, If you don’t have the pymongo library in your system then install it by using the below command:

pip3 install pymongo

After installing the library now you can use it. First of all, import the library then connect to the server so you can use MongoDB in python, and for storing the images create a database.

from pymongo import MongoClient

connection = MongoClient("localhost", 27017)

database = connection['DB_NAME']

MongoDB by default runs on port 27017. DB_NAME here you can use any name for the database.

In the next step, we will use the gridfs library for storing the images into the MongoDB database.

import gridfs

#Create a object of GridFs for the above database.
fs = gridfs.GridFS(database)

#define an image object with the location.
file = "C:/Users/user/Pictures/dog.jpeg"

#Open the image in read-only format.
with open(file, 'rb') as f:
    contents =

#Now store/put the image via GridFs object.
fs.put(contents, filename="file")
Storing the images into MongoDB database using python
Storing the images into MongoDB database using python

After executing the above code you can see the output:

Storing the images into MongoDB database
Storing the images into the MongoDB database
  • In the right-side of the output, you can see database Images is created succcessfully.
  • In the database there two sub-folders named as fs.chunks and fs.files.
  • Our image stored in fs.files folder with ObjectID((‘612727d8e71b2de49ac00734’) where you can see all the detail related to the image like _id, filename, md5, chunkSize, length and uploadDate.

You may also like reading the following articles.

In this tutorial, we have learned “How to store images in MongoDB database” using different ways with examples. These are the following topics that we covered in this tutorial

  • Can we store image in MongoDB?
  • Storing images in MongoDB using python

Leave a Comment