Ordered insert
Ordered behavior
var sportsCollection = bettingDatabase
.GetCollection<Sport>(Constants.SportsCollection);
// Sport title is the identifier _id field
var sports = new List<Sport>
{
new Sport { Title = "Soccer", TotalEvents = 100 },
new Sport { Title = "Basketball", TotalEvents = 50 },
new Sport { Title = "Tennis", TotalEvents = 60 },
};
// Insert 3 documents
await sportsCollection.InsertManyAsync(sports);
// Now try to add 3 more
// The 2nd though
var sportsToAdd = new List<Sport>
{
new Sport { Title = "Volleyball", TotalEvents = 12 },
// This should cause an error and stop further processing
new Sport { Title = "Basketball", TotalEvents = 44 },
// This will never be inserted
new Sport { Title = "Formula 1", TotalEvents = 67 },
};
try
{
await sportsCollection.InsertManyAsync(sportsToAdd);
}
catch (MongoBulkWriteException e)
{
Utils.Log(e.Message);
}Unordered behavior
Last updated