The spec for GridFS mentions that filename is optional. However, the Java driver GridFSBucket seems to require this field.
For example, this fs.files collection using GridFS (note the missing filename field):
> db.fs.files.find() { "_id": ObjectId("5950700998e91439516f0abe"), "chunkSize": 261120, "uploadDate": ISODate("2017-06-26T02:23:05.529Z"), "length": 8466, "md5": "c09d698b0e623e5cc05cb05d49002a2d" }
This Java code snippet will fail to download the file:
GridFSBucket bucket = GridFSBuckets.create(database); bucket.downloadToStream(new ObjectId("5950700998e91439516f0abe"), System.out);
The error displayed is:
org.bson.BsonInvalidOperationException: Document does not contain key filename
However the same code in Python will work:
fs = gridfs.GridFSBucket(conn.test) print fs.open_download_stream(bson.ObjectId("5950700998e91439516f0abe")).read()
There seems to be a discrepancy in GridFS behaviour between the Java driver and the Python driver.