title: pymongo - how to append values to array?
body: <p>I am trying to create a database if it is not there already in <code>mongo</code>, and then create a collection in the database; then insert/update a document (which contains two keys, called <code>erp</code> and <code>dataset</code> whose value is a list of strings) in the collection. I know how to upsert a document like,</p>
  
  <pre><code>self.connection = pymongo.MongoClient(host=db_host, port=db_port)
  self.connection.datasets.datasets.update_one({'erp': 'erp1'},
                                               {'$set': {'data_set': ['database1']}},
                                               upsert=True)
  </code></pre>
  
  <p>When the document is inserted into mongo for the 1st time, create a list with string(s) as values for the field 'data_set', but how to maintain/update the list of strings that whenever a new string comes in, just append the existing list for <code>data_set</code>. </p>
  
  <p>UPDATE.
  The working query</p>
  
  <pre><code>connection.erp_datasets.erp_datasets.update_one({'erp_name': 'erp1'},
                                                  {'$push': {'data_set': 'database1'}}, upsert=True)
  </code></pre>