c# mongodb driver 2.0 'InsertManyAsync' ContinueOnError

2,352 views
Skip to first unread message

den21

unread,
Apr 23, 2015, 7:24:12 AM4/23/15
to mongodb...@googlegroups.com
Hey guys,

I'm looking for something like the 'InsertFlags.ContinueOnError' for InsertBatch of previous c# mongo driver. Anyone knows here?


Using mongodb c# 2.0 driver, I wanted to insert multiple documents(index set on a key). I'm using the 'InsertManyAsync', but when a duplicate based on index occurs, I get an error "A bulk write operation resulted in one or more errors."

I can't find anything in documentation that will just continue inserting the rest of the documents, ignoring the duplicate/error.

sample json to insert:
[
{"u":"http://test1.com"}, 
{"u":"http://test2.com"}, 
]

//creating the index:
await collection.Indexes.CreateOneAsync(Builders<BsonDocument>.IndexKeys.Ascending("u"), new CreateIndexOptions { Unique = true});

//inserting
await collection.InsertManyAsync(xx /*the json above*/);

Robert Stam

unread,
Apr 23, 2015, 8:24:04 AM4/23/15
to mongodb...@googlegroups.com
The corresponding option in the new API is the IsOrdered flag in the InsertManyOptions class. When the server executes inserts in order it stops at the first error. When the server is allowed to execute the inserts in any order, it does not stop when an error occurs.

The code would look like this:

    var options = new InsertManyOptions { IsOrdered = false };
    await collection.InsertManyAsync(documents, options);

This was a change in how insert commands are sent to the server. All drivers changed. You can see the documentation for the MongoDB Shell's version of insert here:



--
You received this message because you are subscribed to the Google Groups "mongodb-csharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-cshar...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

den21

unread,
Apr 23, 2015, 9:24:18 AM4/23/15
to mongodb...@googlegroups.com
Ah thanks, and thanks for the link!

I saw that on the InsertManyOptions c# driver documentation, but it really wasn't that informative: 
http://api.mongodb.org/csharp/2.0/html/P_MongoDB_Driver_InsertManyOptions_IsOrdered.htm

"Gets or sets a value indicating whether the requests are fulfilled in order."

Didn't mention anything about continue on error something.

Anyways, Thanks again!
Reply all
Reply to author
Forward
0 new messages