Journeyman
Journeyman

Reputation: 10281

How do I overwrite a file in 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?

Upvotes: 7

Views: 3393

Answers (1)

Masker
Masker

Reputation: 76

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)

Upvotes: 5

Related Questions