title: Duplicate key error in Mongoose' findOneAndUpdate and upsert
body: <p>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 <a href="https://stackoverflow.com/questions/18111141/mongoose-upsert-duplicate-key-error">here</a> 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.</p>
  <p>To explain the behaviour more:</p>
  <ol>
  <li>When the csv file contains an email address that's already stored in MongoDB before I run my script, findOneAndUpdate works perfectly</li>
  <li>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 <code>{ MongoError: E11000 duplicate key error collection: db.accounts index: email_1 dup key: { : &quot;xxxxx@yahoo.com&quot; }</code></li>
  <li>I wrote <em>sometimes</em> above because sometimes (although less often) I don't and everything works as it should be.</li>
  </ol>
  <p>Codes:</p>
  <pre><code>for (let i = 0; i &lt; 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( ... );
     ...
  }
  </code></pre>