I need to insert or update multiple documents of 150,000+, total in size of 20+ MB atomically using MongoDB Transaction.
var client = new MongoClient(ConnectionString);
using (var session = await client.StartSessionAsync())
{
try
{
// execute async operations using the session
var database = client.GetDatabase(DatabaseName);
var carCollection = database.GetCollection<Car>(CarName);
var writeModels = new List<WriteModel<Car>>();
foreach (var item in Cars)
{
var filter = Builders<Car>.Filter...;
var update = Builders<Car>.Update...
var upsert = new UpdateOneModel<Car>(filter, update) { IsUpsert = true };
writeModels.Add(upsert);
}
await carCollection .BulkWriteAsync(writeModels);
}
catch
{
await session.AbortTransactionAsync(); // now Dispose on the session has nothing to do and won't block
throw;
}
await session.CommitTransactionAsync();
}
MongoDB imposes a max limit of 100,000 document on each batch, and 16MB in size, if I remember correctly,
Question
How to get around the limitation of the batch update.
https://mongodb.github.io/mongo-csharp-driver/2.13/reference/driver/crud/sessions_and_transactions/