9
votes

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:

Cannot encode object: ObjectId('51861bc79bb6550f2b98be23')

... "/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')

I have no idea why this is happening. Did anybody else encounter this error, or does somebody have an idea what could cause it?

Update: The object I am trying to save has following structure:

{
   'is_open': true,   // boolean
   'data': {
      'user_id': ObjectId(...), // ObjectId
      'user_type': 1,           // Integer
   }
}

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.

Thanks, Gerald

2
Is there anything else about the object you're trying to save that you could share? What type of data does it contain? - WiredPrairie
I have update the description of the problem. @lovesh: I don't think that problem is related. - nutrina
to revive an old question: what are you giving to the ObjectId constructor? Is it a string? Is there any chance it has some odd characters in it, like a trailing null char, newline, ...? - drevicko
@drevicko: it turned out that the configuration of the wsgi web app where this was implemented was the problem (I think the WSGIProcessGroup and WSGIApplicationGroup where not set right). The document I tried to save was fine. I found the information for this here: api.mongodb.org/python/current/examples/…. There was however an older version of pymongo that was running, not 2.7 which is documented at the link above. - nutrina

2 Answers

15
votes

Python integers are stored as arbitrary precision numbers, which is not supported by Mongodb. You need to convert them into normal int64 or string objects first.

2
votes

If you're not using a native type in your document (string, integer, date, boolean, etc) you need to make sure it's encoded correctly. If you don't properly encode your non-standard data types there's no guarantee that it can be inserted into PyMongo. You're probably hitting an edge case where it refuses to construct your new object into a PyMongo document.

PyMongo: Custom Types