insert_many(..., bulk_options={ordered : false}) throws exception #118
Description
Activity
PR #157 is merged.
insert_manycan now preserve the native reply even when a duplicate key makes the call throw:reply = Ref{Mongoc.BSON}() try Mongoc.insert_many(collection, documents; bulk_options=Mongoc.BSON("ordered" => false), reply=reply) catch err err isa Mongoc.BSONError || rethrow() isassigned(reply) && @show(reply[]) end
For the reported example, the retained reply shows two inserted documents and a write error at index zero. These indices are zero-based positions in the input. The existing error still signals that the batch was not wholly successful; callers can inspect the partial result and choose how to handle it. Setup errors before native execution leave the reference unchanged.
This was reproduced and verified against a real MongoDB server on Julia 1.6.7 and 1.13.0, including reply lifetime after garbage collection. All PR and merged-master CI jobs pass. The change is available on master; no new package release has been made. See the current API documentation.
AI disclosure: This work was prepared with assistance from OpenAI Codex.
db.collection.insertMany() with the option "ordered = false" can be used to insert multiple documents while ignoring intermediate insertion errors due to duplicate errors. See https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#execution-of-operations :
If ordered is set to false and an insert fails, the server continues inserting records. Documents may be reordered by mongod to increase performance. Applications should not depend on ordering of inserts if using an unordered insertMany().The julia function insert_many(..., bulk_options=BSON("ordered" => false)) throws an exception although the non-duplicate documents have been inserted. The problem with throwing an exception here is that we loose access to the results which contains information about which documents have been inserted and which not.
minimal example:
The example shows that the non-duplicate documents have been added and the duplicate document has not been replaced as expected, although the insert_many function has thrown an exception.