title: mongoimport failing inconsistently with "The _id field cannot be changed from"
body: <p>I have problems importing data into mongodb with. I have a <code>test.json</code> file like this:</p>
  
  <pre><code>{"_id":{"s":{"$numberLong":"38851448"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38853194"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38760498"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"39099662"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38855558"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38760487"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38760488"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"39099663"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38851450"},"a":5},"someKey":"someValue"}
  {"_id":{"s":{"$numberLong":"38853546"},"a":5},"someKey":"someValue"}
  </code></pre>
  
  <p>I try to import it with following command:</p>
  
  <pre><code>mongoimport --type json --db test --collection coll --file test.json --upsert
  </code></pre>
  
  <p>The import fails almost always with the same error message:</p>
  
  <pre><code>2015-08-27T17:02:15.510+0200    error inserting documents: The _id field cannot be changed from {_id: { s: 38851448, a: 5 }} to {_id: { a: 5, s: 38851448 }}.
  2015-08-27T17:02:15.511+0200    error inserting documents: The _id field cannot be changed from {_id: { a: 5, s: 38760487 }} to {_id: { s: 38760487, a: 5 }}.
  </code></pre>
  
  <p>It's frustrating that this error isn't even reproducable. The importer seems to change the order of the properties for <code>_id</code>, but I don't know why, is this a BUG that should be reported or is already known? Or is there 
  a problem I don't see.</p>
  
  <p>I even tried to change the order of <code>s</code> and  <code>a</code> in <code>_id</code>, but the problem stays the same.</p>
  
  <p>If I trie to run the exact same import command with exactly the same datafile multiple times the documents with errors change and one time it imported all rows as expected, but just one time:</p>
  
  <p><a href="https://i.stack.imgur.com/OGh7u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OGh7u.png" alt="enter image description here"></a></p>
  
  <p>For completeness: I'm using mongo 3.0.5 on Mac OS X 10.10.5 installed by homebrew.</p>
  
  <p><strong>UPDATE</strong>:
  I have created a Ticket with mongoDB-Team: <a href="https://jira.mongodb.org/browse/TOOLS-894" rel="nofollow noreferrer">TOOLS-894</a></p>
  
  <p><strong>UPDATE2</strong>:</p>
  
  <p>I tried not to use _id but id for my unique key:</p>
  
  <pre><code>{"id":{"s":{"$numberLong":"38851448"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38853194"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38760498"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"39099662"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38855558"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38760487"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38760488"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"39099663"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38851450"},"a":5},"someKey":"someValue"}
  {"id":{"s":{"$numberLong":"38853546"},"a":5},"someKey":"someValue"}
  </code></pre>
  
  <p>and import this with:</p>
  
  <pre><code>mongoimport --type json --db test --collection coll --file test.json --upsertFields id
  </code></pre>
  
  <p>Now I don't get any errors, but after two imports I have 15 rows in the collection instead of 10. Again because of the order of the properties in <code>id</code>.</p>