0

Currently I'm struggeling with querying data from a MongoDB GridFS by using the filename.

Current situation

I'm uploading data to the MongoDB by using a GridFSBucket and UploadFromBytesAsync which returns an ObjectId for referencing the data and takes a filename as parameter.

The Problem

The oposit of UploadFromBytesAsync is DownloadAsBytesAsync which should be used for querying the data from GridFS. But this method only accepts an ObjectId and not the unique filename which is also passed when using UploadFromBytesAsync. But I want to query the data by using the filename.

Possible solution

My Idea was, to create a collection, which stores the GridFS ObjectId and GridFS filename, to map them. So when querying data, i will search for the filename in the collection and then using the ObjectId to get the bytes from GridFS. The filename property would also get an index.

Or should I directly query from GridFS using the unique filename? And is the filename indexed than?

EDIT!

Oh, i think the problem is solved by using DownloadAsBytesByName. Thank you very much!

mu is too short
  • 426,620
  • 70
  • 833
  • 800
BendEg
  • 20,098
  • 17
  • 57
  • 131

1 Answers1

1

As long as file names are unique - you can use get by file name.

To ensure there is only one file, you can use storage option: OverWrite

You can create index on fs collection to include fileName field.

-- edit the call with overwrite loos like:

_db.GridFS. Upload(OverWrite(fileName, binaryStream))
profesor79
  • 9,213
  • 3
  • 31
  • 52