I am facing some problems with findOneAndUpdate when the input coming from my csv file has two or more rows that have the same email address. Email address is set to unique in my model and I thought findOneAndUpdate would let me handle duplication in my csv file. The codes are below. Read here that it's because the fields in my query (in this case email) is also one of the fields I want to create in case the record is not found. I'm not sure if this is true. And in any case email is my identifier so it has to be in there.
To explain the behaviour more:
{ MongoError: E11000 duplicate key error collection: db.accounts index: email_1 dup key: { : "[email protected]" }
Codes:
for (let i = 0; i < accounts.length; i++) {
let query = {'email': accounts[i].email};
let accountHolderDoc = {
email: accounts[i].email,
name: {
first: accounts[i].accountHolderFName,
last: accounts[i].accountHolderLName,
},
};
promise = AccountHolder
.findOneAndUpdate(
query, {$set: accountHolderDoc}, {upsert: true, new: true})
.then(function(accountHolder) { ... })
.catch( ... );
...
}
This post suggests that searching for and saving a non-existing document are not atomic, which means between the searching and the saving, another query could have yielded a not found result for the same search criteria. The only solution then, it seems, is to ensure that if duplicate key errors are handled, for example, by reapplying MongoDB operations to the document that was thrown out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With