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;
})