javascript - Duplicate key error in Mongoose' findOneAndUpdate and upsert - Stack Overflow

I am facing some problems with findOneAndUpdate when the input ing from my csv file has two or more row

I am facing some problems with findOneAndUpdate when the input ing 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:

  1. When the csv file contains an email address that's already stored in MongoDB before I run my script, findOneAndUpdate works perfectly
  2. However, when I have two records in the csv file that share the same email address but no record of this email address is stored in MongoDB prior to running the script, I sometimes get a duplicate key error like so { MongoError: E11000 duplicate key error collection: db.accounts index: email_1 dup key: { : "[email protected]" }
  3. I wrote sometimes above because sometimes (although less often) I don't and everything works as it should be.

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

I am facing some problems with findOneAndUpdate when the input ing 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:

  1. When the csv file contains an email address that's already stored in MongoDB before I run my script, findOneAndUpdate works perfectly
  2. However, when I have two records in the csv file that share the same email address but no record of this email address is stored in MongoDB prior to running the script, I sometimes get a duplicate key error like so { MongoError: E11000 duplicate key error collection: db.accounts index: email_1 dup key: { : "[email protected]" }
  3. I wrote sometimes above because sometimes (although less often) I don't and everything works as it should be.

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( ... );
   ...
}
Share Improve this question edited Dec 17, 2021 at 18:36 Henry Ecker 35.7k19 gold badges47 silver badges64 bronze badges asked Sep 23, 2017 at 10:18 fabfab 4827 silver badges21 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744984826a4604549.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信