PyMongo trouble encoding numbers

3,616 views
Skip to first unread message

jonhanke

unread,
Nov 27, 2010, 11:25:18 AM11/27/10
to sage-support, jonh...@gmail.com
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

William Stein

unread,
Nov 27, 2010, 9:02:22 PM11/27/10
to sage-s...@googlegroups.com
Use int(..) instead of Integer. Mongo only encodes standard python types.

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> 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

Robert Bradshaw

unread,
Nov 29, 2010, 11:10:27 PM11/29/10
to sage-s...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages