
OPERATIONAL DEFECT DATABASE
...

...
Problem pretty same as in SERVER-38583 There is only one BulkWriteError with dup key after inserting duplicated data within a transaction, even if data contain many of them. Problem reproduces with Mongo version 4.2.3, 4.2.10, 4.4.1. No info regarding another versions.
JIRAUSER1279917 commented on Thu, 29 Aug 2024 07:58:06 +0000: Hi sory to get this topic from the past but i think it needs to be solved. Yes, this is working as designed, but design is wrong since you are forced to do duplicate checks when using transactions/session. It should at least retrieve the list of docs with errors in the insertBulk. Here i explain why and give some more context https://www.mongodb.com/community/forums/t/insertbulk-aborts-transaction-even-if-unordered/291697 Ty in advance. AgCaliva. JIRAUSER1264483 commented on Fri, 21 Jan 2022 13:16:44 +0000: Agreed that it is intended (according to the documentation), but it is not useful. Why not returning all the errors instead of a single one? I would like to have a list of errors to be able to process them later in a correct way, instead of retrying just to find out the second error, and so on. JIRAUSER1257066 commented on Mon, 9 Nov 2020 17:43:41 +0000: Hi dmitry.kiziloff@gmail.com, Thank you for your ticket submission. My investigation leads me to believe that the behavior you described, i.e. only a single document is returned in the BulkWriteError, is intended. This is further detailed in the Error Handling inside Transactions section of the db.collection.bulkWrite() docs: Inside a transaction, the first error in a bulk write causes the entire bulk write to fail and aborts the transaction, even if the bulk write is unordered. This tells me that as soon as a duplicate error is reached inside of a transaction, there are no further attempts to insert additional documents, preventing further discovery of duplicates. Best, Edwin
start session, open transaction in this session write documents with several duplicated keys expected: write errors about all duplicates actual: 1 write error @Test public void transactionalCase() { ClientSession session = dbFactory.getSession(ClientSessionOptions.builder().build()); Document document1 = Document.parse("{_id:1, value: 1}"); Document document2 = Document.parse("{_id:2, value: 2}"); Document document3 = Document.parse("{_id:1, value: 3}"); Document document4 = Document.parse("{_id:1, value: 4}"); session.startTransaction(); try { collection.insertMany(session, List.of(document1, document2, document3, document4), new InsertManyOptions().ordered(false)); Assert.fail("insertMany should fail"); // session.commitTransaction(); } catch (MongoBulkWriteException e) { session.abortTransaction(); Assert.assertEquals(e.getWriteErrors().size(), 2, "Expected to have 2 dup key errors:"); // fails here! } finally { session.close(); } }
MongoDB Integration
Learn more about where this data comes from
Bug Scrub Advisor
Streamline upgrades with automated vendor bug scrubs
BugZero Enterprise
Wish you caught this bug sooner? Get proactive today.