title: Is there a way in mongodb to have insertOne fail to create duplicate indexed documents without throwing an error?
body: <p>I wanted to avoid creating duplicate documents, so upon searching how to do this, the first solution I found was <a href="https://stackoverflow.com/a/24125275/9400421">this answer</a>:</p>
  
  <pre><code>db.collection.update(doc, doc, {upsert:true})
  </code></pre>
  
  <p>But this is really slow compared to <code>insertOne</code>, so I looked to the <a href="https://stackoverflow.com/a/24123426/9400421">second answer</a>:</p>
  
  <pre><code>db.collection.createIndex( { propertyName: 1 }, {unique:true} )
  </code></pre>
  
  <p>This seems like a way better solution in terms of performance, but the problem is rather than just not inserting the duplicate, it throws an error:</p>
  
  <p><code>(node:97200) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: main.dir index: path_1 dup key: { propertyName: "propertyName123" }</code></p>
  
  <p>Is there a setting to make it not throw an error or do I need to use a try catch here?</p>