Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidDocument: Cannot encode object: ObjectId('51861bc79bb6550f2b98be23')

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

like image 365
nutrina Avatar asked Aug 25 '13 15:08

nutrina


2 Answers

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

like image 113
blakev Avatar answered Nov 10 '22 01:11

blakev


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.

like image 23
Xiaoji Liu Avatar answered Nov 10 '22 00:11

Xiaoji Liu