title: Change type from ObjectId to string
body: <p>I have some corrupted documents in my mongodb so I want to write a repair script which should fix it. However, it doesn't work.</p>
  
  <pre><code>db.History.find({_id:{ $type: "objectId" }}).forEach( function (x) {   
    x._id = x._id.valueOf(); // convert field to string
    db.History.replaceOne({BId:x.BId, "Version.Version": x.Version.Version}, x);
  });
  </code></pre>
  
  <p>But I get an exception </p>
  
  <blockquote>
    <p>Failed to execute script.</p>
    
    <p>Error: WriteError: The _id field cannot be changed from {_id:
    ObjectId('59cd189e6e5c9e1ca90b072c')} to {_id:
    "59cd189e6e5c9e1ca90b072c"}.</p>
  </blockquote>
  
  <p>How can it be done? I now have an exception in my app layer because it cannot deserialize ObjectId field as string.</p>