Storing images in MongoDB
You can use a database to store pictures and other small images in a database table. Such picture files can be processed more efficiently on a file server.
However, when image data is stored in a binary field, it can only be accessed by applications that stream raw picture data to and from that field.
The MongoDB GridFS specification is a viable option for storing fairly large files in MongoDB. It ensures that the files are broken down into manageable bits and saved in the database.
This article explains the process of saving and retrieving binary files in MongoDB.
GridFS in MongoDB
GridFS is a standard for storing and retrieving files that are larger than the 16 MB limit set by BSON. GridFS splits files into parts, or chunks, and stores each part separately rather than as a single document.
Store a metadata JSON document for each item in Couchbase, along with at most a small thumbnail.
The document contains the information about that object in your application that you need and references a purpose-built object store such as S3, a file system, or HDFS. So you'll have the best of both worlds.
In this MongoDB tutorial, the issue of storing images in MongoDB database is discussed in detail. Also, you will learn about different ways to save images and retrieve images from MongoDB database efficiently.
Storing images in MongoDB using GridFS
You can store images in a MongoDB database by creating a schema using Mongoose. The schema is model.js
defined by creating a file.
Data type Buffer
is used to store images in the database form of array.
There are three ways to store images:
Since large files are difficult for end users to access, storing binary files in a database makes it easier to distribute across numerous sites. It is worth noting that before saving photos to a database, you should consider its benefits.
Storing images in MongoDB using Python
This section will discuss how to store images in MongoDB using Python.
There are two libraries you can use for this:
pip3 install pymongo
You can now use the library after installing it. But first, import the library, connect to the server to use MongoDB in Python, and set up a database to store photos.
from pymongo import MongoClient
connection = MongoClient("localhost", 27017)
database = connection['DB_NAME']
MongoDB runs by default on port 27017. You can DB_NAME
specify any name for the database in .
The GridFS library stores photos in a MongoDB database in the following stages.
import gridfs
#Create an 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"
with open(file, 'rb') as f:
contents = f.read()
fs.put(contents, filename="file")
The above code shows how to save photos in a MongoDB database using Python.
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
List all collections in MongoDB Shell
Publish Date:2025/04/29 Views:156 Category:MongoDB
-
When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c
Querying for non-null values in MongoDB
Publish Date:2025/04/29 Views:139 Category:MongoDB
-
This MongoDB article will explain how to query for non-null values in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers
Shutting down with code:100 error in MongoDB
Publish Date:2025/04/29 Views:54 Category:MongoDB
-
This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know
SELECT COUNT GROUP BY in MongoDB
Publish Date:2025/04/29 Views:75 Category:MongoDB
-
In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation
Differences between MongoDB and Mongoose
Publish Date:2025/04/29 Views:80 Category:MongoDB
-
This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s
Install MongoDB using Homebrew
Publish Date:2025/04/29 Views:161 Category:MongoDB
-
MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a
Create a MongoDB dump of the database
Publish Date:2025/04/29 Views:63 Category:MongoDB
-
In this MongoDB article, you’ll get a walkthrough of Mongodump and Mongorestore , how to use them, and some simple examples of backing up and restoring your collections using both tools. mongodump Commands in MongoDB Mongodump is a tool t
Get the size of the database in MongoDB
Publish Date:2025/04/29 Views:88 Category:MongoDB
-
When working in MongoDB, do you know the size of your database? Today, we will learn how to get the size of a database in MongoDB using show dbs the command and the method. db.stats() Get the size of the database in MongoDB We can show dbs;
Grouping values by multiple fields with MongoDB
Publish Date:2025/04/29 Views:100 Category:MongoDB
-
MongoDB Group by Multiple Fields is used to group values by multiple fields using various methods. One of the most efficient ways to group various fields present in MongoDB documents is by using $group the operator, which helps in per