Can we get the count of files stored in GridFS
using PyMongo other than using list function?
Also, when I try the list()
method under gridfs
, it gives me an empty list although when there are files in the DB. Am able to retrieve the files using get()
method by using the _id
.
Does the list() function return list of all the files stored under gridfs
db
if we save the files without filenames and depend on the _id
value instead.
Code:
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 the list()
method implementation.
def list(self):
"""List the names of all files stored in this instance of
:class:`GridFS`.
.. 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]
Demo:
>>> import pprint
>>> from pymongo import MongoClient
>>> import gridfs
>>> client = MongoClient()
>>> db = client.demo
>>> fs = gridfs.GridFS(db)
>>> fs.put('img.jpg', encoding='utf-8')
ObjectId('573af0960acf4555437ceaa9')
>>> 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')
ObjectId('573af1740acf4555437ceaab')
>>> fs.list()
['img.jpg']
>>> 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.
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