Search code examples
c#mongodbmongodb-.net-drivergridfs

How to enumerate files in GridFS with C#


The GridFS api is quite simple compared to Mongo's and there are calls to read, write, stream, delete files, but I can't find any way to find the list of files, nor even how to find if a specific file exists.

I know I could keep a list of files in a table, but I am looking for a way to discover the files from scratch.

Some tools, like Studio3T are able to enumerate the files, so there is a way.


Solution

  • You can use Find method on GridFSBucket type as described here:

    var bucket = new GridFSBucket(database);
    var filter = Builders<GridFSFileInfo>.Filter.Empty;
    
    using (var cursor = bucket.Find(filter))
    {
        var fileNames = cursor.ToList().Select(x => x.Filename);
    }