using System; using System.Buffers; using System.Buffers.Text; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; namespace TestApplication { class Program { static async Task Main(string[] args) { var client = new MongoClient("mongodb://127.0.0.1:27017"); var database = client.GetDatabase("TestDb"); using (var session = await client.StartSessionAsync()) { session.StartTransaction(); await Task.WhenAll( WriteA(database, session), WriteB(database, session) ); await session.CommitTransactionAsync(); } Console.Read(); } private static Task WriteA(IMongoDatabase db, IClientSessionHandle session) { return db.GetCollection("CollectionA").InsertOneAsync(session, new CollectionAEntity { Name = "SomeStringA" }); } private static Task WriteB(IMongoDatabase db, IClientSessionHandle session) { return db.GetCollection("CollectionB").InsertOneAsync(session, new CollectionBEntity { Name = "SomeStringB" }); } } public class CollectionAEntity { public ObjectId Id { get; set; } public string Name { get; set; } } public class CollectionBEntity { public ObjectId Id { get; set; } public string Name { get; set; } } }