Skip to content

Instantly share code, notes, and snippets.

@g0t4
Created December 12, 2012 20:01
Show Gist options
  • Save g0t4/4271057 to your computer and use it in GitHub Desktop.
Save g0t4/4271057 to your computer and use it in GitHub Desktop.
Mongodb editing file information with c# driver
//Easier to just pull the file collection and edit the files documents, just be careful, example:
public void SaveMetadata(string id, MetadataJson metadata)
{
var gridFs = FilesContext.GetGridFs();
var files = gridFs.Database.GetCollection(gridFs.Settings.FilesCollectionName);
var file = files.FindOneById(new BsonObjectId(id));
var metadataDocument = file["metadata"].AsBsonDocument;
metadataDocument.Set("comment", metadata.comment ?? string.Empty);
if (metadata.name.IsNotNullOrWhiteSpace())
{
file.Set("filename", metadata.name);
}
files.Save(file);
}
public class MetadataJson
{
public string name { get; set; }
public string comment { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment