1. b.93z.org
  2. Notes

pymongo and binary data

Let’s imagine we need to save zlib-compressed string.

>>> import zlib
>>> s = '...'
>>> compressed = zlib.compress(s)  # type(compressed) == str

Here’s a problem: an exception will be raised if we’ll try to save it into DB as-is. pymongo uses unicode, while compressed is an instance of str. And we can’t just .decode('utf-8') it.

>>> collection.insert({'c': compressed})
---------------------------------------------------------------------------
InvalidStringData                         Traceback (most recent call last)
.../env/lib/python2.6/site-packages/pymongo/collection.pyc in insert(self, doc_or_docs, manipulate, safe, check_keys)
    211 
    212         self.__database.connection._send_message(
--> 213             message.insert(self.__full_name, docs, check_keys, safe), safe)
    214 
    215         ids = [doc.get("_id", None) for doc in docs]
InvalidStringData: strings in documents must be valid UTF-8

But we can use Binary from pymongo.binary:

>>> from pymongo.binary import Binary
>>> collection.insert({'c': Binary(compressed)})

Now it works.

© 2008–2017 93z.org