I wanted to avoid creating duplicate documents, so upon searching how to do this, the first solution I found was this answer:
db.collection.update(doc, doc, {upsert:true})
But this is really slow compared to insertOne
, so I looked to the second answer:
db.collection.createIndex( { propertyName: 1 }, {unique:true} )
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:
(node:97200) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: main.dir index: path_1 dup key: { propertyName: "propertyName123" }
Is there a setting to make it not throw an error or do I need to use a try catch here?