Sharath Sharath - 6 months ago 8
Python Question

The gridfs "list" method returns empty list with non empty collection

Can we get the count of files stored in

using PyMongo other than using list function?

Also, when I try the
method under
, it gives me an empty list although when there are files in the DB. Am able to retrieve the files using
method by using the
Does the list() function return list of all the files stored under
if we save the files without filenames and depend on the
value instead.


client = pymongo.MongoClient(connect=False)
grid_db = client['gridDB']
fs = gridfs.GridFS(grid_db)
# Save an image
img_identifier = fs.put(img, encoding="utf-8")
# List the files stored
print fs.list()
'''[] Console shows empty array'''


This is the expected result. The reason is that you didn't set a value for the field filename during insert (put), however the list method returns distinct values for the filename field in the collection. So it returns empty list if the field doesn't exist in the collection. See list implementation

def list(self):
    """List the names of all files stored in this instance of
    .. versionchanged:: 3.1
       ``list`` no longer ensures indexes.
    # With an index, distinct includes documents with no filename
    # as None.
    return [
        name for name in self.__files.distinct("filename")
        if name is not None]


>>> import pprint  
>>> from pymongo import MongoClient
>>> import gridfs
>>> client = MongoClient()
>>> db = client.demo
>>> fs = gridfs.GridFS(db)
>>> fs.put('img.jpg', encoding='utf-8')
>>> fs.list()
>>> pprint.pprint(db['fs.files'].find_one())
{'_id': ObjectId('573af0960acf4555437ceaa9'),
 'chunkSize': 261120,
 'encoding': 'utf-8',
 'length': 7,
 'md5': '82341a6c6f03e3af261a95ba81050c0a',
 'uploadDate': datetime.datetime(2016, 5, 17, 10, 21, 11, 38000)}

As you can see, the field filename doesn't exist in your document. Now let's pass in the filename argument:

>>> client.drop_database(db) # drop our demo database
>>> fs.put('img.jpg', encoding='utf-8', filename='img.jpg')
>>> fs.list()
>>> pprint.pprint(db['fs.files'].find_one())
{'_id': ObjectId('573af1740acf4555437ceaab'),
 'chunkSize': 261120,
 'encoding': 'utf-8',
 'filename': 'img.jpg',
 'length': 7,
 'md5': '82341a6c6f03e3af261a95ba81050c0a',
 'uploadDate': datetime.datetime(2016, 5, 17, 10, 24, 53, 449000)}

As you can see, list returns a list of 'filename' values.