title: Insert Binary data into Mongo field in pymongo
body: <p>I'm trying to do something that I feel aught to be pretty trivial, so forgive me if there's some easy solution out there elsewhere.</p>
  
  <p>I'm writing tests for some content indexing and for this I'm trying to insert some binary data (a pdf) into a mongo collection that I have. However, I'm having a good deal of trouble with this.  This is the current state of my relevant code</p>
  
  <pre><code>pseudo_file = StringIO()
  pdf = pisa.CreatePDF("This is a test", pseudo_file)
  test = {"data": pseudo_file}
  test.update({"files_id": {"name": "random_asset_name"}, "category": "asset"})
  self.chunk_collection.insert(json.dumps(test))
  </code></pre>
  
  <p>I managed to find an old thread on the Pymongo google group addressing this problem (<a href="https://groups.google.com/forum/#!topic/mongodb-user/uBAbY1wdQbs" rel="noreferrer">https://groups.google.com/forum/#!topic/mongodb-user/uBAbY1wdQbs</a>), but I can't seem to find the <code>Binary</code> object that was used to fix that problem and it doesn't seem to be included in Python (I'm using 2.7)</p>
  
  <p>Right now the problem I'm getting is that the <code>StringIO</code> object is not JSON serializable, which is sensible, but pymongo needs a valid utf8 object passed to it. I tried using a base64 encoding of the <code>StringIO.getvalue()</code>, and just directly serializing the same value. </p>
  
  <p>Of course the pdf is not value utf8, so I'm wondering if there's another way to have pymongo recognize that I am sending it a raw binary. Any help is appreciated.</p>