public void TestInsertBatchContinueOnError()
{
var collection = Configuration.TestCollection;
collection.Drop();
collection.CreateIndex(IndexKeys.Ascending("x"), IndexOptions.SetUnique(true));
var batch = new BsonDocument[]
{
new BsonDocument("x", 1),
new BsonDocument("x", 1), // duplicate
new BsonDocument("x", 2),
new BsonDocument("x", 2), // duplicate
new BsonDocument("x", 3),
new BsonDocument("x", 3) // duplicate
};
// try the batch without ContinueOnError
try
{
collection.InsertBatch(batch);
}
catch (WriteConcernException)
{
Assert.AreEqual(1, collection.Count());
Assert.AreEqual(1, collection.FindOne()["x"].AsInt32);
}
// try the batch again with ContinueOnError
if (_server.BuildInfo.Version >= new Version(2, 0, 0))
{
try
{
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
collection.InsertBatch(batch, options);
}
catch (WriteConcernException)
{
Assert.AreEqual(3, collection.Count());
}
}
}