Note, however, that Mongo does not support arbitrary precision
integers, so if you need more than 64 bits than you'll have to use a
string.

- Robert

On Sat, Nov 27, 2010 at 6:02 PM, William Stein <[email protected]> wrote:
> Use int(..) instead of Integer.  Mongo only encodes standard python types.
>
> On Saturday, November 27, 2010, jonhanke <[email protected]> wrote:
>> Hi,
>>
>> I'm trying to use PyMongo in Sage 4.6, and am having trouble encoding
>> numbers in dictionary documents, though strings seem to work well.
>> Any comments are appreciated!  Thanks,
>>
>> -Jon
>>  =)
>>
>> =============================================
>>
>> sage:  C = pymongo.Connection("localhost:29000")
>> sage:  col = C.db_test.c_test
>> sage: col
>> Collection(Database(Connection('localhost', 29000), u'db_test'),
>> u'c_test')
>> sage: col.find_one()
>> sage:
>> sage: col.insert({ "a" : str(1)})
>> ObjectId('4cf12d39e1382313ae000010')
>> sage:
>> sage: col.insert({ "a" : Integer(1)})
>> ---------------------------------------------------------------------------
>> InvalidDocument                           Traceback (most recent call
>> last)
>>
>> /home/jonhanke/290_Mongo_code/<ipython console> in <module>()
>>
>> /usr/local/sage-4.6/local/lib/python2.6/site-packages/pymongo-1.9-
>> py2.6-linux-x86_64.egg/pymongo/collection.pyc in insert(self,
>> doc_or_docs, manipulate, safe, check_keys, **kwargs)
>>     268         self.__database.connection._send_message(
>>     269             message.insert(self.__full_name, docs,
>> --> 270                            check_keys, safe, kwargs), safe)
>>     271
>>     272         ids = [doc.get("_id", None) for doc in docs]
>>
>> /usr/local/sage-4.6/local/lib/python2.6/site-packages/pymongo-1.9-
>> py2.6-linux-x86_64.egg/pymongo/message.pyc in insert(collection_name,
>> docs, check_keys, safe, last_error_args)
>>      65     data = __ZERO
>>      66     data += bson._make_c_string(collection_name)
>> ---> 67     bson_data = "".join([bson.BSON.encode(doc, check_keys) for
>> doc in docs])
>>      68     if not bson_data:
>>      69         raise InvalidOperation("cannot do an empty bulk
>> insert")
>>
>> /usr/local/sage-4.6/local/lib/python2.6/site-packages/pymongo-1.9-
>> py2.6-linux-x86_64.egg/bson/__init__.pyc in encode(cls, document,
>> check_keys)
>>     460         .. versionadded:: 1.9
>>     461         """
>> --> 462         return cls(_dict_to_bson(document, check_keys))
>>     463
>>     464     def to_dict(self, as_class=dict, tz_aware=False):
>>
>> InvalidDocument: Cannot encode object: 1
>> sage:
>> sage: col.insert({ "a" : 1})
>> ---------------------------------------------------------------------------
>> InvalidDocument                           Traceback (most recent call
>> last)
>>
>> /home/jonhanke/290_Mongo_code/<ipython console> in <module>()
>>
>> /usr/local/sage-4.6/local/lib/python2.6/site-packages/pymongo-1.9-
>> py2.6-linux-x86_64.egg/pymongo/collection.pyc in insert(self,
>> doc_or_docs, manipulate, safe, check_keys, **kwargs)
>>     268         self.__database.connection._send_message(
>>     269             message.insert(self.__full_name, docs,
>> --> 270                            check_keys, safe, kwargs), safe)
>>     271
>>     272         ids = [doc.get("_id", None) for doc in docs]
>>
>> /usr/local/sage-4.6/local/lib/python2.6/site-packages/pymongo-1.9-
>> py2.6-linux-x86_64.egg/pymongo/message.pyc in insert(collection_name,
>> docs, check_keys, safe, last_error_args)
>>      65     data = __ZERO
>>      66     data += bson._make_c_string(collection_name)
>> ---> 67     bson_data = "".join([bson.BSON.encode(doc, check_keys) for
>> doc in docs])
>>      68     if not bson_data:
>>      69         raise InvalidOperation("cannot do an empty bulk
>> insert")
>>
>> /usr/local/sage-4.6/local/lib/python2.6/site-packages/pymongo-1.9-
>> py2.6-linux-x86_64.egg/bson/__init__.pyc in encode(cls, document,
>> check_keys)
>>     460         .. versionadded:: 1.9
>>     461         """
>> --> 462         return cls(_dict_to_bson(document, check_keys))
>>     463
>>     464     def to_dict(self, as_class=dict, tz_aware=False):
>>
>> InvalidDocument: Cannot encode object: 1
>>
>> --
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to 
>> [email protected]
>> For more options, visit this group at 
>> http://groups.google.com/group/sage-support
>> URL: http://www.sagemath.org
>>
>
> --
> William Stein
> Professor of Mathematics
> University of Washington
> http://wstein.org
>
> --
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to 
> [email protected]
> For more options, visit this group at 
> http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to