title: 'str' object does not support item assignment when inserting to pymongo collection
body: <p>RHEL 6.5, python 2.6.6</p>
  
  <p>I'm trying to write decoded messages in to a MongoDB collection.  The "decoded message" is received in the form of a dictionary.  Within that dictionary the keys are all strings, and the values can be strings, integers, <code>datetime.time</code>, <code>datetime.datetime</code>, or <code>Decimal</code> types.</p>
  
  <p>I've written an <code>on_message</code> function which is called with a parameter <code>context</code> contining the dictionary to be saved to MongoDB.  I've also written a lambda to convert some types that aren't JSON serializeable.</p>
  
  <pre><code>dthandler = lambda obj: (
      str(obj) 
      if isinstance(obj, Decimal)
      or isinstance(obj, datetime.time)
      else None
  )
  
  class Decoder(decoder.base.Base):
  
    # ...
  
    def on_message(self, context, payload):
  
      """  Write decoded message to DB
      """
      msg = dict(context)
      try:
  
          print self.mongo_coll
  
          # This is just a test to confirm the connection is established properly.
          # I am able to see this value in the DB using Robomongo after this insert 
          # call
          self.mongo_coll.insert({'foo': 'john'})
  
          x = json.dumps(context, default=dthandler)
          print x
          self.mongo_coll.insert(x)
      except errors.OperationFailure as op:
          print "Operation Exception"
  
      except Exception as ex:
          print "Serialization Exception (mongo/write.py)"
          exs = str(ex)
          print exs
  
          print '^^^^^'
  </code></pre>
  
  <p>The call to <code>print self.mongo_coll</code> yields:</p>
  
  <blockquote>
    <p>Collection(Database(MongoClient('localhost', 27017), u'itch'),
    u'test')</p>
  </blockquote>
  
  <p>the call to <code>print x</code> yields (all results are similar):</p>
  
  <blockquote>
    <p>{"msg-type-name": "AddOrderAttributed", "next-expect-msg-seq-num":
    3023982, "mold-num-msgs": 34, "recv-timestamp": null, "ord-ref-num":
    4365786, "msg-type": "F", "recv-time-nano": 2523000, "raw-price":
    781200, "msg-seq-num": 3023981, "shares": 100, "buy-sell": "B",
    "stock": "RNR", "attribution": "RBCM", "timestamp": "09:30:00.002189",
    "price": "78.12", "tracking-number": 1, "recv-time-sec": 1419431400,
    "msg-size": 40, "mold-seq-num": 3023959, "stock-locate": 6281,
    "mold-session": "000006896B", "msg-payload": "00 28 46 18 89 00 01 1f
    1a ce fb 57 59 00 00 00 00 00 42 9d da 42 00 00 00 64 52 4e 52 20 20
    20 20 20 00 0b eb 90 52 42"}</p>
  </blockquote>
  
  <p>However the call to <code>self.mongo_coll.insert(x)</code> yields an <code>Exception</code>:</p>
  
  <blockquote>
    <p>Serialization Exception (mongo/write.py) </p>
    
    <p>'str' object does not support item assignment  </p>
    
    <p>^^^^^</p>
  </blockquote>
  
  <p>I'm quite confused especially given that there is no mention of <code>str</code> in my code, except in my exception handler and within <code>dthandler</code>.</p>
  
  <p>What am I doing wrong?</p>
  
  <hr>
  
  <p>Per edits and answers, I've made a couple modifications.  Alas, I'm still having trouble.  Here's the new code:</p>
  
  <pre><code> def on_message(self, context, payload):
      """  Write decoded message to DB
      """
      msg = dict(context)
      try:
          print self.mongo_coll
          self.mongo_coll.insert(msg)
          x = json.dumps(context, default=dthandler)
          print self.mongo_coll
          print x
          self.mongo_coll.insert(msg)
      except errors.OperationFailure as op:
          print "Operation Exception"
      except Exception as ex:
          traceback.print_exc()
          print "Serialization Exception (mongo/write.py)"
          exs = str(ex)
          print exs
  
          print '^^^^^'
  </code></pre>
  
  <p>...and the output:</p>
  
  <pre><code>Collection(Database(MongoClient('localhost', 27017), u'itch'), u'test')
  Traceback (most recent call last):
    File "./decoder/mongo/write.py", line 69, in on_message
      self.mongo_coll.insert(msg)
    File "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 409, in insert
      gen(), check_keys, self.uuid_subtype, client)
  InvalidDocument: Cannot encode object: Decimal('65.1')
  Serialization Exception (mongo/write.py)
  Cannot encode object: Decimal('65.1')
  ^^^^^
  </code></pre>