Search code examples
mongodbpython-sacred

Accessing files in Mongodb


I am using sacred package in python, this allows to keep track of computational experiments i'm running. sacred allows to add observer (mongodb) which stores all sorts of information regarding the experiment (configuration, source files etc). sacred allows to add artifacts to the db bt using sacred.Experiment.add_artifact(PATH_TO_FILE).

This command essentially adds the file to the DB.

I'm using MongoDB compass, I can access the experiment information and see that an artifact has been added. it contains two fields: 'name' and 'file_id' which contains an ObjectId. (see image)

I am attempting to access the stored file itself. i have noticed that under my db there is an additional sub-db called fs.files in it i can filter to find my ObjectId but it does not seem to allow me to access to content of the file itself.

object id under .files

file_id under artifact/object


Solution

  • MongoDB file storage is handled by "GridFS" which basically splits up files in chunks and stores them in a collection (fs.files).

    Tutorial to access: http://api.mongodb.com/python/current/examples/gridfs.html