title: MongoDB Java: how to get write error document?
body: <p>I'm inserting 1000 documents in mongodb 3.6 at once using an unordered bulk via mongodb java api 3.6.1 and the method <code>insertMany(List&lt;Document&gt;)</code>. </p>
  
  <pre><code>try {
      collection.insertMany(docs);
  } catch (MongoBulkWriteException e) {
      // e is :
     // Bulk write operation error on server myserver.com:27011. Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key error collection: foodb.bar index: bar.id_1 dup key: { : 1 }', details={ }}].     
  }
  </code></pre>
  
  <p>Let's say only one document triggered a duplicate key error. All others 999 documents were successfully inserted.<br>
  <strong>How can I know which document triggered the error ?</strong>   </p>
  
  <p>Of course when getting the duplicate key error, I could search my list of documents with the <code>bar.id</code> that was duplicated but it's inconvenient (it means parsing the WriteError message...) and if there is two documents with the same <code>bar.id</code> in the <code>List&lt;Document&gt;</code>, it's almost impossible to know which triggered the error without asking the db.</p>
  
  <p>Thanks</p>