• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:8 mins read
You are currently viewing MongoDB Put an Image File in a JSON Object

There are many ways to insert or put an image file in a JSON object, and in this article, we will explore methods for putting an image file in a JSON object using MongoDB. These methods are required because it is not recommended to store a file directly in a JSON object in MongoDB due to performance and size issues.

Advertisements

1. Put an image with Base64 encoding in JSON object

Base64 encoding is a common method for representing binary data as ASCII text. To store an image in a JSON object using Base64 encoding, we can read the image file into a buffer using the fs module. And encode the buffer as a Base64 string. We can then create a new JSON object that includes the Base64 string and insert the JSON object into the database.

Consider the following example. We have specified the fs module to read the file and the base64 command to convert it to a Base64 string. Then, we create a JSON object and add the Base64 string as a property to the key image along with the name and age field. Finally, we inserted the JSON object into MongoDB with the insertOne() method.


#Adding an image with Base64 encoding
const fs = require('fs');
const imageBuffer = fs.readFileSync('G:\Student1.PNG');
const base64Image = imageBuffer.toString('base64');
const JSON = {
  name: 'Jenny william',
  age: 21,
  image: base64Image
};
db.student.insertOne(JSON);

The output indicates that the JSON object with the Base64-encoded image file is now stored in MongoDB.

MongoDB Put image file in JSON object

More discussion about this part can be found here.

2. Put an image with BSON Binary type in JSON object

Furthermore, we can use the BSON Binary type to put an image file in a JSON object. The BSON Binary type is a data type in MongoDB that represents binary data.

To store an image in a JSON object using the BSON Binary type, we can read the image file into a buffer using the fs module. After that, we create a new BSON Binary object with the buffer. We can then create a new JSON object that includes the BSON Binary object and insert the JSON object into the database.

Consider the following example, we have first loaded the BSON module to access it into MongoDB.

Along with that, we have specified the fs module to read the StudentProfile.PNG file from the specified path into a buffer using the readFileSync method. Then, creates a BSON.Binary object binaryData from the buffer using the BSON.Binary constructor.

After that, create a JSON object with the binary data. Finally, it inserts the JSON object into the image collection using the insertOne method.

.
#Adding an image with BSON Binary type 
const BSON = require('bson');
const fs = require('fs');
const imageBuffer = fs.readFileSync('G:\StudentProfile.PNG');
const binaryData = new BSON.Binary(imageBuffer);a
const json = {
  file: 'StudentProfile.PNG',
  data: binaryData
};
db.student.insertOne(json);

The image is inserted into the JSON object as verified in the following output.

MongoDB Put image file in JSON object

3. Put an image with a URL in JSON object

Another way to store images in a MongoDB document is to store the URL of the image as a string in the document. This method is often used when the images are hosted on a different server or service. Consider the following example, we have created the json object that is specified with the image URL. Then, insert the json object into the collection student.


#Adding an image with a URL in JSON object
const json = {
  name: 'Profile1',
  url: 'https://www.studentPortal.com/StudentProfile.PNG'
}
db.student.insertOne(json);

The output shows that an image URL is stored in a MongoDB JSON object.

MongoDB Put image file in JSON object

4. Put multiple images

Moreover, we have seen how to put an image file in a JSON object with the prior examples. However, we can put multiple image files at the same time in a JSON file.

Considering the following example. We loaded two image files StudentProfile.PNG and StudentCard.PNG into variables mgData1 and imgData2, respectively, and read them into Buffer objects. Then, we convert each Buffer into a Base64-encoded string and create a json object. This object contains an array of documents, each containing the name of the image and its Base64-encoded data. Finally, we insert the documents into the collection student using the insertMany() method.


#Adding multiple images
const fs = require('fs');
const img1Data = fs.readFileSync('G:\StudentProfile.PNG');
const img2Data = fs.readFileSync('G:\StudentCard.PNG');
const image1Base64 = img1Data.toString('base64');
const image2Base64 = img2Data.toString('base64');
const json = [
  { name: 'Student Image1', image: image1Base64 },
  { name: 'Student Image2', image: image2Base64 }
];
db.student.insertMany(json);

The output shows here that we have inserted the image file in a JSON object successfully.

MongoDB Put image file in JSON object

5. Conclusion

In conclusion, we are now familiar with the way to put an image file in a JSON object by exploring the above method. The choice of the method relies on the particular use case and requirements. As each method has advantages and disadvantages of its own.