Baruch Oxman
Baruch Oxman

Reputation: 1636

Opening a MongoDB GridFS by name with C# Driver

In pymongo, there is an option to open a GridFS with a specific collection name. E.g. mygridfs = gridfs.GridFS(db, collection = mycolc).

I cannot find a similar option in the MongoDB C# driver (official MongoDB latest driver version).

As a result if I want to share GridFS data between Python and C# modules, I can only use the default GridFS in the DB (named 'fs').

Any clues to whether I can somehow access a GridFS with a non-default name in the C# MongoDB driver ?

Upvotes: 4

Views: 504

Answers (1)

Disposer
Disposer

Reputation: 6371

A sample using grid in c#:

var url = new MongoUrl("mongodb://localhost");
var Client = new MongoClient(url);
var Server = Client.GetServer();
var Database = Server.GetDatabase("test");
var collection = Database.GetCollection("test");

var set = new MongoGridFSSettings {UpdateMD5 = true, ChunkSize = 512*1024, VerifyMD5 = false};

// you can set the name here
set.Root = "mycol";
var grid = Database.GetGridFS(set);

// Upload
grid.Upload(@"C:\Users\Hamid\Desktop\sample.txt", "remote");

// Download
grid.Download(@"C:\Users\Hamid\Desktop\sample2.txt", "remote");    

Upvotes: 5

Related Questions