Ordered insert
Ordered behavior
When adding multiple documents using the InsertMany
method and if an error occurs with one of the documents to be added, by default MongoDB will return an error without processing the remain documents in the array. For example, if you try to insert 3 documents and the 2nd violates a unique index key, then the 1st one will be added in the collection but 2nd and 3rd won't. Let's see it in action.
Ordered insert doesn't rollback on failure but doesn't continue either
Unordered behavior
You can configure your multiple documents insertions, to continue even if when some of the documents cause errors, by informing MongoDB not to use the default ordered insert behavior. You can do this by passing an InsertManyOptions
argument in the InsertMany
method and setting IsOrdered = false
.
InsertMany
will cause an MongoBulkWriteException
exception with both ordered and unordered behavior
Last updated