Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating metadata for gridfs file object

I use GridFS as follows:

connection = MongoClient(host='localhost')
db = connection.gridfs_example
fs = gridfs.GridFS(db)
fileId = fs.put("Contents of my file", key='s1')

After files are originally stored in GridFS, I have a process that computes additional metadata respective to the contents of the file.

def qcFile(fileId):
    #DO QC
    return "QC PASSED"

qcResult = qcFile(fileId)

It would have been great if I could do:

fs.update(fileId, QC_RESULT = qcResult)

But that option does not appear to exist within the documentation. I found here (the question updated with solution) that the Java driver appears to offer an option to do something like this but can't find its equivalent in python gridfs.

So, how do I use pymongo to tag my file with the newly computed metadata value qcResult? I can't find it within the documentation.

like image 478
Spade Avatar asked Sep 05 '25 17:09

Spade


1 Answers

GridFS stores files in two collections: 1. files collection: store files' metadata 2. chunks collection: store files' binary data

You can hack into the files collection. The name of the files collection is 'fs.files' where 'fs' is the default bucket name.

So, to update the QC result, you can do like this:

db.fs.files.update({'_id': fileId}, {'$set': {'QC_RESULT': qcResult}})

like image 87
Serenity Avatar answered Sep 07 '25 17:09

Serenity