Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch inserts using ContinueOnError

I'm using the following code to do a batch insert using the C# driver. I have a unique index, and I want it to fail silently if I try to insert a record that isn't unique.

Even though I have InsertFlags.ContinueOnError set, I still get an error on the InsertBatch call. If I swallow the error as I have shown below, everything works ok. But this certainly feels wrong.

var mio = new MongoInsertOptions {Flags = InsertFlags.ContinueOnError};
// newImages is a list of POCO objects
try
{
    _db.GetCollection("Images").InsertBatch(newImages, mio);
}
catch (WriteConcernException)
{           
}
like image 463
Alex Kilpatrick Avatar asked Mar 21 '26 10:03

Alex Kilpatrick


1 Answers

Are you using version 1.8 of the csharp Mongo driver?

If so, try upgrading to version 1.8.1 which contains a fix for the following two issues:

  • InsertBatch fails when large batch has to be split into smaller sub batches

  • InsertBatch throws duplicate key exception with too much data...

So your inserts could succeed, but the driver is still throwing an exception on bulk insert operations due to the bug above.

And this exception doesn't originate from the database itself, explaining why the inserts succeed but you still need to catch the exception afterwards - i.e. the db is in fact respecting your ContinueOnError flag but the driver throws an exception anyway afterwards.

like image 77
Ryan Weir Avatar answered Mar 24 '26 03:03

Ryan Weir