Hi,
I’m uploading many files in GridFS with temporary filenames and then I’m updating the filenames in a transaction. At the end of the transaction if there aren’t errors I commit the transaction so I expect that all filenames are updated.
But this not happens: only the filename of the first file is updated! why?
I know that GridFS not support the transactions but I suppose that I can update, with a transaction, the metadata in the .files collection.
This is the code to reproduce the issue:
private static void UpdateOneTest(IMongoDatabase database, string filepath)
{
var bucket = new GridFSBucket(database, new GridFSBucketOptions { BucketName = "videos" });
var collection = database.GetCollection<BsonDocument>("videos.files");
var id1 = new ObjectId("507f1f77bcf86cd799439181");
var id2 = new ObjectId("507f191e810c19729de8638a");
FileStream fs = File.OpenRead(filepath);
using (var session = database.Client.StartSession())
{
session.StartTransaction();
//******* Upload file1 and update filename in transaction ********
bucket.UploadFromStream(id1, $"tmp_file1", fs, new GridFSUploadOptions());
var filter1 = Builders<BsonDocument>.Filter.Eq("_id", id1);
var update1 = Builders<BsonDocument>.Update.Set("filename", "file1");
var res1 = collection.UpdateOne(session, filter1, update1);
Debug.Assert(res1.ModifiedCount == 1, "filename of file1 not updated!"); // ok
fs.Seek(0, SeekOrigin.Begin);
//******* Upload file2 and update filename in transaction ********
bucket.UploadFromStream(id2, $"tmp_file2", fs, new GridFSUploadOptions());
var filter2 = Builders<BsonDocument>.Filter.Eq("_id", id2);
var doc = collection.Find(filter2).FirstOrDefault();
Debug.Assert(doc != null, "file not exists"); // OK: the file exists
var update2 = Builders<BsonDocument>.Update.Set("filename", "file2");
var res2 = collection.UpdateOne(session, filter2, update2);
Debug.Assert(res2.ModifiedCount == 1, "filename of file2 not updated!"); // ----- KO ---- and MatchedCount is 0
session.CommitTransaction();
}
}
Only If I move the uploalds before start the transaction the filenames are all updated.
Same issue if I try to update a metadata instead of filename.
I’m using MongoDB Community Edition version 6 with Replica Set enabled and with only the primary node.
Thanks, Nunzio