title: Append item to MongoDB document array in PyMongo without re-insertion
body: <p>I am using MongoDB as the back-end database for Python web application (PyMongo + Bottle). Users can upload files and optionally 'tag' these files during upload. The tags are stored as a list within the document, per below:</p>
  
  <pre><code>{
      "_id" : ObjectId("561c199e038e42b10956e3fc"),
      "tags" : [ "tag1", "tag2", "tag3" ],
      "ref" : "4780"
  }
  </code></pre>
  
  <p>I am trying to allow users to append new tags to any document. I came up with something like this:</p>
  
  <pre><code>def update_tags(ref, new_tag)
      # fetch desired document by ref key as dict
      document = dict(coll.find_one({'ref': ref}))
      # append new tag
      document['tags'].append(new_tag)
      # re-insert the document back into mongo
      coll.update(document)
  </code></pre>
  
  <p>(fyi; <code>ref</code> key is always unique. this could easily be <code>_id</code> as well.)
  It seems like there should be a way to just update the 'tags' value directly without pulling back the entire document and re-inserting. Am I missing something here?</p>
  
  <p>Any thoughts are greatly appreciated :)</p>