-
Notifications
You must be signed in to change notification settings - Fork 20
Description
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.