0

I have an image saved in MongoDB in the table with following method:

@staticmethod
def upload_member_image(member_id, raw_image):
    Database.insert("files", {"_id": uuid.uuid4().hex,
                              "member_id": member_id,
                              "image": Binary(raw_image.read())
                              })

which calls:

@staticmethod
def insert(collection, data):
    Database.DATABASE[collection].insert_one(data)

record is saved properly and I can see it in mongo console:

db.files.count()
1
> db.files.find()
{ "_id" : "41dbbe5f5d6b49b4b8cb5ed806aa8110", "member_id" : "114f4ff3d8b743a7bbc88a7caf2be928", "image" : BinData(0,"/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gNzUK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhM
 ...

the file itself is pretty tiny, it's size is: 53.2 KB (54,541 bytes)

However when I try to read it from the database I got nothing, no data is being read. I'm trying to do this with regular method:

@staticmethod
def find(collection, query):
    return Database.DATABASE[collection].find(query)

What is the problem here? Does binary data require 'special treatment or so?

To be more specific here above database call throws an exception:

return Database.DATABASE[collection].find_one(query)
TypeError: 'NoneType' object is not subscriptable

and the query is:

{"member_id": member_id}

with member_id value exactly as expected

smoczyna
  • 489
  • 6
  • 18
  • 1
    [This question](https://stackoverflow.com/questions/42276665/retrieve-stored-image-from-mongodb-using-python) should be useful, I don't have any experience with this kind of problem. – Eric Mar 25 '19 at 12:58
  • After few more tests of my own I decided to go this way indeed, pymongo seems to call db asynchronously and fixing my problem by hand is whole lot harder task than using GridFS. – smoczyna Mar 26 '19 at 12:44

0 Answers0