I have an image with the following metadata:
> db.fs.files.find().pretty()
{
"_id" : ObjectId("4576874577342672346"),
"chunkSize" : 262144,
"user_name" : "my name",
"filename" : "image.jpg",
"length" : 7103,
"uploadDate" : ISODate("2014-01-23T13:31:48.155Z"),
"user_email" : "[email protected]",
"md5" : "1234567890"
}
>
I want to delete the image from Python (PyMongo).
The documentation on delete()
seems to stipulate that the only accepted parameter in the delete()
function is the file_id
:
http://api.mongodb.org/python/current/api/gridfs/#gridfs.GridFS.delete
Programmatically, I have the following values available that can be matched in the files metadata:
How do I either:
file_id
(through use of the above values if necessary) or file_id
?Additionally, I am only currently testing with single chunk files, if interacting with larger files in the future will deleting by file_id
or other metadata remove all associated chunks as well?
Here's something I just tried without thinking if it's necessary or the best way to do it, but it works.
So programatically I could have the _id
available from querying on the files metadata:
Python Shell:
>>> import pymongo
>>> import os
>>> hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']
>>> conn = pymongo.MongoClient(host=hostname)
>>> db = conn.grid_files
>>> collection = db.fs.files
>>> result = collection.find_one({"user_email":"[email protected]","name":"my name","filename":"image.jpg"})
>>> result['_id']
ObjectId('52e119c47091447a86891d98')
# now use the _id to delete the file
>>> files_id = result['_id']
>>> import gridfs
>>> fs = gridfs.GridFS(db)
>>> fs.delete(files_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With