1

I am trying to keep a collection with unique keys. Data to be inserted comes from various distributed places (although individual documents are immutable), and could possibly contain duplicates. I was hoping to simply insert records and suppress duplicate key errors using continueOnError, but the duplicate key error is still thrown. The code looks like this...

fetchStatuses(statusId)
  .then(results => connection
    .then(db => db.collection('statuses').ensureIndex({id: 1}, {
      unique: true, dropDups: true
    })
      .then(() => db.collection('statuses').insert(results, {continueOnError: true, safe: true}))
      .then(response => {
        winston.info(`Inserted ${response.insertedCount} statuses into mongo`);
        return results;
      })
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
Andy Hume
  • 40,474
  • 10
  • 47
  • 58

1 Answers1

3

You may want to try an unordered insert like described in the docs:

The following example performs an unordered insert of three documents. With unordered inserts, if an error occurs during an insert of one of the documents, MongoDB continues to insert the remaining documents in the array.

db.products.insert(
   [
     { _id: 20, item: "lamp", qty: 50, type: "desk" },
     { _id: 21, item: "lamp", qty: 20, type: "floor" },
     { _id: 22, item: "bulk", qty: 100 }
   ],
   { ordered: false }
)

As far as I understand the docs, there won't be thrown an error in this case but instead there is set a special property of the result object

If the insert() method encounters a non-write concern error, the results include the WriteResult.writeError field

WriteResult({
   "nInserted" : 0,
   "writeError" : {
      "code" : 11000,
      "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.foo.$_id_  dup key: { : 1.0 }"
   }
})
DAXaholic
  • 33,312
  • 6
  • 76
  • 74