title: Upserting 'rows' mongodb
body: <p>I have a remote processes that generate JSON objects and send them to a server that stores then in a MongoDB. I would like to store these objects in what I would call rows (coming from an SQL background) within a ‘master’ collection for example:</p>
  
  <p>Results (Main Collection)</p>
  
  <p>Results.Day
      Results.Day.1.Data
  Results.2
      Results.Day.2.Data
  etc</p>
  
  <p>I am using an upsert because as the day proceeds some of the results will be regenerated by the remote processes so will need updating. At present I can upsert Results.Day.1 however when I attempt to upsert Results.Day.2 I get an error saying I have a duplicate key.</p>
  
  <p>I am using the following query for my first upsert:</p>
  
  <p>{ _id: ‘UUID’, Results.Day: { “$eq”: 1 } }</p>
  
  <p>and for the second upsert:</p>
  
  <p>{ _id: ‘UUID’, Results.Day: { “$eq”: 2 } }</p>
  
  <p>Is it even possible to do this, and if so how can I do it? I would appreciate your thoughts on this. It is obvious I am no expert on NoSQL but from my limited understanding it would appear that I need to insert the 'rows' as arrays, but how I do this I do not know.</p>
  
  <p>I have access to the source code to both the client and server so can change what is needed in order to accomplish this, so I would really appreciate help on this.</p>
  
  <p>I am using for the Server</p>
  
  <p>MongoDB v3.0.4 on Linux and the legacy-0.0-26compat c++ driver.
  Windows 7 x64 and the same driver for the Client.</p>
  
  <p>EDIT:</p>
  
  <p>Ok, based on the comments below and on further reading I see that I simply can't change the _id key. However, if I hand craft a document to mimic what I am trying to achieve in code I can load it in to the database without error :</p>
  
  <pre><code>{
  "_id": "aebc483c-5832-45ac-8a43-16b2c591b520",
  "Results":
  {
  "Day":
      {
          "19":
          {
              "Data":
              {
                  Blah
              }
          },
      "201":
          {
              "Data":
              {
                  More Blah
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But when I upsert using :</p>
  
  <pre><code>{
  "_id": "aebc483c-5832-45ac-8a43-16b2c591b520",
  "Results":
  {
  "Day":
      {
          "20":
          {
              "Data":
              {
                  Blah
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>It replaces what I already have in the database instead of inserting Day 20 into the collection, needless to say this is not what I want. Thoughts anyone?</p>