How to delete an image file from GridFS by file metadata?
Question: 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@email.com",
        "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:

  * user_name 
  * filename 
  * user_email 

How do I either:

  * Get the `file_id` (through use of the above values if necessary) or 
  * Delete the file based on metadata fields other than `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?


Answer: 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@email.com","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)
    

