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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With