Images and metadata on MongoDb

I’m storing images in the database along with their width, height and format. If I want to get the width and height data before downloading the image to help with page layout I can query and just select those fields.

To return just selected fields MongoDB has to deserialize the BSON and return the requested data. Does the fact there’s a very large object as part of the BSON affect performance? Am I going to achieve better performance storing the image metadata in one collection and the image buffer in a separate collection?

Hello, welcome to the MongoDB community!

The fact that you insert the image into MongoDB really affects the performance of the database. For these cases, the ideal is to store the image in S3/CloudFront and place the path that points to the image in the database. This way you don’t have performance problems due to the image being too large.

I’m available.

1 Like

I wanted to add my 2 cents before it is closed but it came to fast.

Here is it.

I think there are cases where it might be more efficient to have the image inside MongoDB. The overhead of opening the connection to a third party service may be bigger than having MongoDB serve the image. If the meta data (width, height, format) is covered by the index the image does not have to be fetched. Even if the image data is fetch it might be faster because when the image is send over in the 2nd access it is already in mongod’s cache, while it may not be in the 3rd party server.

I do not say it is better to have it in mongod. I just say that it may and it is something that has to be tested with your own data. I do not think it is an easy answer.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.