title: immutable _id error when performing MongoDB bulkWrite replaceOne on first attempt only
body: <p>I'm working on a little web application that will crawl and update baseball standings by day and track teams positions (among other things) over time.</p>
  <p>I have an API I grab all of this from and a collection in MongoDB that stores all the team data and information for the current day. Right now I just run this manually but eventually it'll be automated to run at like 3am or whenever.</p>
  <p>The API supplies a unique ID for each team that never changes. So what I'm doing is I'm taking in the team data from the API. Passing it to a function that then extracts the teams data (there is other data from the response object I don't need), puts it into an object for replacement, and then wherever that team ID exist in the collection its document is replaced in a bulkWite.</p>
  <pre><code>async function currentStandings(db,team_standings,callback){
    const current_standings = db.collection('current_standings');
  
    let replacePool = [];
  
    for(const single_team of team_standings.data.standing){
      let replaceOnePusher = {
        replaceOne: {
          &quot;filter&quot;: {&quot;team_id&quot;: single_team.team_id},
          &quot;replacement&quot;: single_team
        }
      }
  
      replacePool.push(replaceOnePusher);
    }
  
    await current_standings.bulkWrite(replacePool);
  
    callback();
  }
  </code></pre>
  <p>However when I execute this code for the first time each day I get an error reading <code>BulkWriteError: After applying the update, the (immutable) field '_id' was found to have been altered to _id: ObjectId('5f26e57b6831761ac840bf1d')</code> (not the same ID every day) and if I look in Compass the data isn't updated. If I immediately run the script again, it goes through successfully without error. Refreshing the data in compass generates the correct data.</p>
  <p>Can someone explain to me what is going wrong here? This is actually my first time using MongoDB since I wanted to learn it and this pet project seemed like a good place to start.</p>