title: Upsert-Updating multiple fields of a document including lists and nested lists using pyMongo
body: <p>I have a collection with documents like this:</p>
  
  <pre><code>{
  "_id"    : "1234567890",
  "area"   : "Zone 63",
  "last_state" : "Cloudy",
  "recent_indices" : [
          21,
          18,
          33,
          ...
          38
          41
    ],
  
  "Report_stats" : [
   {
          "date_hour" : "2017-01-01 01",
          "count"     : 31
       },
   {
          "date_hour" : "2017-01-01 02",
          "count"     : 20
       },
          ...
       {
          "date_hour" : "2018-08-26 13",
          "count"     : 3
       }     
    ]
  }
  </code></pre>
  
  <p>which is supposed to be updated based on some online real-time reports
  and assume each report looks like this:</p>
  
  <pre><code>{
   'datetime' : '2018-08-26 13:48:11.677635',
   'areas'    : 'Zone 3; Zone 45; Zone 63',
   'status'   : 'Clear',
   'index'    : '33'
   }
  </code></pre>
  
  <p>Now I have to update the collection in way that:</p>
  
  <ol>
  <li>Each time that a new 'area' (say Zone 1025) shows up on the report, a new document adds to keep the related data</li>
  <li>New 'index' adds to list "recent_indices" while "last_state" updates to 'status'</li>
  <li>based on what the 'datetime' is, the respective "Report_stats.count" increments by 1 or a new "Report_stats" document ('datetime' with an hour resolution, where its 'count' is 1) inserted.</li>
  </ol>
  
  <p>The way to do each of these updates separately, is somehow obvious, the problem is: How can I do all these simultaneously in a single update/upsert task?</p>
  
  <p>I tried to use <code>update_one</code> and <code>find_one_and_update</code>(as well as <code>update</code> and <code>find_and_modify</code>) using pyMongo, but it was not possible (for me at least) to resolve the problem.</p>
  
  <p>So I started to wonder if there possibly is a simple/single task to do so, or I should start trying to fix it in a different way altogether.</p>
  
  <p>Can you please help me how to do this or (since there is a lot of data being gathered and therefore should be processed) suggest a low-cost alternative?</p>
  
  <p>Thank you!</p>