Working with Image Files in MongoDB

Hello,
I am new to MongoDB and I have a question around storing image files in Mongo.

I have read and I understand that there are 3 different ways to store the file:

GridFS

Inline: Basically inbed the picture in the actual document… (I know I need to be mindful of the 16M size limit for the document)

Reference: This would entail storing a Reference URL instead of the actual file (At least that is my understanding).

While I am familiar with the second two methods, I am not at all familiar with the GridFS method.

Here is what I need to do… I am designing an online e-commerce website that will sell products… I initially wanted to include a photo gallery of up to 3 to 5 images of the product. Because of this, I am concerned with using the inline method and I lean toward the reference method. I just wanted to check and see if I am on the right track.

If Reference is the most optimal method, is there any recommendations on what storage to use with the URL? I am developing on Azure.

Thanks.

Respectfully,

David Thompson

Hi @David_Thompson ,

You are correct in leaning towards referencing a hosted URL in MongoDB rather then using GridFS. Today the image hosting solutions like S3 or others are so much easier to use and cheaper so storing this data in MongoDB does not make real sense.

The GridFS is a general solution when storing Binary data in MongoDB , but I would recommend it for systems that does not have access to API’s and services therefore have no other way of storing the data rather then in the database…

Ty
Pavel

2 Likes

Thank you… I have one more question that I need guidance on.

Currently I am storing my user in a collection… I wanted to have two addresses stored with each user. One for physical address and the other for shipping. I am currently looking at embedding the address object into the User collection.

My current schema is like this.

User Collection
_id
first_name
last_name
phone
email
Address (This is an object from the address class)
physical
shipping

There are other fields, but for brevity sake I limited them to the bare necessary ones.

Currently I can create a User object and load that into the database using a creatUser method and I pass it both the address object and the user object.

I can get the address objects (both addresses) to load and update in the database.

Where I am struggling is I created a getUser method using the email address of the user. I can get the object to the cursor so I know there is something there, but I am struggling with getting the information stored in the object. I am trying to find out if I query for the user object using the email address, does that return the user AND the address object embedded? If so… How do I access all the information in the record.

I have tried the
for obj in user:
print(obj.get(user.first_name)

This doesn’t return anything.

Any suggestions?

Hi @David_Thompson ,

What driver are you using? Python?

Can you provide more code ?

If the query has embedded documents and you are not projecting specific fields you should get back the entire document and should be able to iterate that as any other json object in your client.

I am sorry for the late response… I figured it out. I am new to Python and I didn’t realize I needed to access fields using so basically I had the object in memory, and to access it I needed to enclose the fields in like this. print(obj[‘first_name’]) where obj is the variable that was created with the query.