简体   繁体   中英

MongoEngine - How to get files from GridFs using ObjectIDs

I have a specific collection of documents when each document holds a file (using GridFS).

I used the aggregate method to retrieve specific documents from that collection.
The aggregate method returned me a list of documents, when each document is represented as a dictionary.
So when I try to get the file from the dict, it returns me an ObjectID Object (and not a file object as returned in the regular querying method).

The question is, how can I get the file object using this ObjectID?
To rephrase it, Is there an easy way to get the actual file object as shown in the documentation using only the ObjectID (without getting involved in the merging of the file chunks in the GridFS)

Thanks.

The solution for this is to use the GridFSProxy object directly and put the file Object ID as the parameter:

import mongoengine
.
.
file_obj = mongoengine.GridFSProxy(FileID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM