Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DominikZians opened this issue Feb 25, 2024 · 0 comments
Open

Comments

@DominikZians
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant