Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overwrite a file in MongoDB Gridfs?

Tags:

c#

mongodb

gridfs

I am writing to the MongoDB gridfs using the following code:

MongoDB.Driver.GridFS.MongoGridFSCreateOptions createOptions = new MongoDB.Driver.GridFS.MongoGridFSCreateOptions();
createOptions.ContentType = Helper.GetFileExtensionFromFilename(clientToSave.LogoFilename);
var gridFsInfo = adminDB.GridFS.Upload(clientToSave.LogoStream, clientToSave.DatabaseName, createOptions);

When I look into Gridfs I can see that files seem to be being added, so I'm ending up with lots of files with the same name. When I read the gridfs it always returns the latest file, so it all works ok, but it seems rather inefficient.

How do I perform a MongoDB gridfs write that overwrites any exiting file with the same name?

like image 921
Journeyman Avatar asked Sep 20 '11 14:09

Journeyman


1 Answers

mongodb does not support this. I think you can delete file with the same name first .just like:

server[dbName].GridFS.Delete(FileName);
server[dbName].GridFS.Upload(localName, FileName)
like image 58
Masker Avatar answered Sep 30 '22 04:09

Masker