Search code examples
c#databasemongodbgridfsdata-storage

GridFS MongoDB retrieve metadata when downloading files


I am trying to use GridFs for uploading and downloading the documents.

var mongoClient = new MongoClient("mongodb://localhost:27017");
var db = mongoClient.GetDatabase("TestDB");

Stream source = new MemoryStream(Content.ToBson());

//Initializing GdridFS conection
GridFSBucket bucket = new GridFSBucket(db);
var options = new GridFSUploadOptions
{
    ChunkSizeBytes = 64512, // 63KB
    Metadata = new BsonDocument
    {
        { "A", "1" },
        { "booleanValue", true }
    } 
};
// file name and source content we are receiving fom post man.
var id = await Bucket.UploadFromStreamAsync("FileName", source, options);  

till here everything is fine. Now I want to download the file based on filename and id. so I use the method.

var x = Bucket.DownloadAsBytesAsync(fileName);

Now when I downloaded it now I want to know all the information regarding the files which was present at the time of uploading. like I want to know the metadata information which I store at the point of upload also the filename, id every piece of information that was present at the time of upload but I don't know how to get it after download all the information. Any help will be appreciated. Regards


Solution

  • Based on https://mongodb.github.io/mongo-csharp-driver/2.10/reference/gridfs/downloadingfiles/ and https://mongodb.github.io/mongo-csharp-driver/2.10/reference/gridfs/findingfiles/#gridfsfileinfo-class: