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