Skip to content

insert_many(..., bulk_options={ordered : false}) throws exception #118

Description

@DominikZians

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:

using Mongoc: BSON, insert_many, insert_one, Client, find

client = Client()
database = client["test_unique"]

# create an index with unique option
reply = write_command(database, BSON(
    "createIndexes" => "test_collection",
    "indexes" => [ BSON("key" => BSON("number" => Int32(1)), "name" => "my_unique_index", "unique" => true) ]
))

collect(find(database["test_collection"]))
# BSON[]

insert_one(database["test_collection"], BSON("number" => 1, "first_entry" => true))

collect(find(database["test_collection"]))
# 1-element Vector{BSON}:
#  BSON("{ "_id" : { "$oid" : "65daa26a27bd2f1ac44fd6a0" }, "number" : 1, "first_entry" : true }")

result = insert_many(database["test_collection"], [BSON("number" => 1),BSON("number" => 2),BSON("number" => 3)], bulk_options=BSON("ordered" => false))
# throws exception due to duplicate insert

collect(find(database["test_collection"]))
# 3-element Vector{BSON}:
#  BSON("{ "_id" : { "$oid" : "65daa26a27bd2f1ac44fd6a0" }, "number" : 1, "first_entry" : true }")
#  BSON("{ "_id" : { "$oid" : "65da9eea27bd2f1ac44fd69c" }, "number" : 2 }")
#  BSON("{ "_id" : { "$oid" : "65da9eea27bd2f1ac44fd69d" }, "number" : 3 }")

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.

Activity

  1. quinnj commented on Oct 4, 2026

    @quinnj
    Member

    PR #157 is merged. insert_many can 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions