I am new to MERN stack and am trying to create a helpdesk ticketing system for IT department of a company.
I want to allow the user to be able to add a new ticket and view all previous tickets on the user interface screen. While filling the ticket, the user must also have the option to upload images regarding the issue or a receipt.
Since I have already used MongoDB to store user data during login, can I store small sized images in binary form in the previous database itself?
If not, where and how else should I store this image data such that it can be retrieved easily?
3
Answers
Reasons why files should be stored in the database:
Reason against putting away records in the database:
increases the database’s size: The level of knowledge required to maintain a database increases in direct proportion to its size. Consequently, maintaining large databases is more challenging than maintaining smaller ones.
serving the files to a website is more difficult.
Cannot make use of cloud storage Cost Distributed storage like ‘AWS S3’ are a lot less expensive
Generally speaking, this is an impractical notion. It will clog up the database files and cause a number of issues with performance. It gets even worse if you put the blobs in a table with a lot of columns.
The DB’s performance will be affected further:
SELECT
on anyBLOB
column, you will always use disk access; however, if you don’t use BLOBs, you can get data straight from RAM (high throughput databases are optimized to fit tables in RAM);Additionally, modern browsers are extremely efficient at serving files from the filesystem, so there is no speed advantage of storing a
blob
over a file system.References:
So for your stack the better option would be to store files in filesystem and can be achieved in two ways:
You can further optimize the
AWS S3
upload by usingpre signed
urls which directly uploads toS3
from your React code. link hereIn general you have two options.
GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB. The files are stored separately on the file system and you don’t have any limits on size.
Store them in the documents. Either you use the Binary Data data type or you store the Base64 value as string. In either case you must convert your binary image into a Base64. If you store it as text then the 16 MiByte size limit corresponds to 12 MiByte max. image size. For
BinData
I don’t know whether the limit of 16 MiByte applies to the binary size or the Base64 string.Of course, you have a third option by storing the file somewhere else and store just the URL in your database.
Yes, you can store small-sized images in binary form directly in your MongoDB database. MongoDB supports the Binary Data type, which allows you to store binary data such as images. You can store the image data as a binary blob within a document in your existing MongoDB database.
To store an image in binary form, you can convert it to a binary format such as Base64 and then store the resulting string in a field of your document. Here’s an example of how you could structure your ticket document in MongoDB to include an image field:
When a user uploads an image, you would convert it to Base64 using a suitable library (such as Buffer in Node.js) and then save the resulting string in the image field of the ticket document.
However, keep in mind that storing large amounts of binary data in MongoDB may not be the most efficient solution in terms of performance and database size. It is strongly recommended to store binary data such as images in a file storage system (e.g., local disk, cloud storage, or a specialized service like Amazon S3) and store the reference to the file in the database instead.
You can follow these steps:
local disk or cloud storage) and generate a unique filename or key
for it.
document in your MongoDB database.
filename or key to fetch the corresponding image file(icluding URL) from the file
storage system and display it in your user interface.
By storing the images separately from the database, you can optimize performance, reduce database size, and make use of specialized storage solutions for handling binary data efficiently.
Consider using libraries or tools like multer or AWS SDK (if you choose cloud storage) to handle file uploads and storage operations more conveniently in your MERN stack application.