title: InvalidDocument: Cannot encode object: ObjectId('51861bc79bb6550f2b98be23')
body: <p>I have been using mongo for a while know (with python, mongo 2.4.4 64 bit, OS X 10.8.2, pymongo 2.5.2, python 2.7.2), and I observed a strange behaviour. Sometimes it throws following exception when trying to insert a document into a collection:</p>
  
  <blockquote>
    <p>Cannot encode object: ObjectId('51861bc79bb6550f2b98be23')</p>
    
    <p>...
    "/Users/nutrina/www/env_pdf_admin_apache/lib/python2.7/site-packages/pymongo/collection.py",
    line 266, in save    return self.insert(to_save, manipulate, safe,
    check_keys, **kwargs)  File
    "/Users/nutrina/www/env_pdf_admin_apache/lib/python2.7/site-packages/pymongo/collection.py",
    line 357, in insert    continue_on_error, self.__uuid_subtype), safe)
    InvalidDocument: Cannot encode object:
    ObjectId('51861bc79bb6550f2b98be23')</p>
  </blockquote>
  
  <p>I have no idea why this is happening. Did anybody else encounter this error, or does somebody have an idea what could cause it?</p>
  
  <p><strong>Update:</strong>
  The object I am trying to save has following structure:</p>
  
  <pre><code>{
     'is_open': true,   // boolean
     'data': {
        'user_id': ObjectId(...), // ObjectId
        'user_type': 1,           // Integer
     }
  }
  </code></pre>
  
  <p>The error is reported for the field *user_id*, but I am pretty sure that value is a valid ObjectId. This is the '_id' of an object (user) from another collection (users). And the save operation succeeds for the same value most of the time. </p>
  
  <p>Thanks,
  Gerald</p>