Python 3 FAQ
Only one intentional change. Instances of are encoded as BSON type 5 (Binary data) with subtype 0.In Python 3 they are decoded back to bytes
. InPython 2 they are decoded to with subtype 0.
For example, let’s insert a bytes
instance using Python 3 thenread it back. Notice the byte string is decoded back to :
- Python 2.7.6 (default, Feb 26 2014, 10:36:22)
- [GCC 4.7.3] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import pymongo
- >>> c = pymongo.MongoClient()
- >>> c.test.bintest.find_one()
- {u'binary': Binary('this is a byte string', 0), u'_id': ObjectId('4f9086b1fba5222021000000')}
There is a similar change in behavior in parsing JSON binary with subtype 0.In Python 3 they are decoded into bytes
. In Python 2 they aredecoded to with subtype 0.
For example, let’s decode a JSON binary subtype 0 using Python 3. Notice thebyte string is decoded to :
- [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
- Type "help", "copyright", "credits" or "license" for more information.
- >>> from bson.json_util import loads
- >>> loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}')
- {u'b': Binary('this is a byte string', 0)}
Instances of pickled using Python 2can always be unpickled using Python 3.
If you pickled an ObjectId using Python 2 and want to unpickle it usingPython 3 you must pass encoding='latin-1'
to pickle.loads:
- Python 3.6.5 (default, Jun 21 2018, 15:09:09)
- [GCC 7.3.0] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import pickle
- >>> from bson.objectid import ObjectId
- >>> oid
- ObjectId('4f96f20c430ee6bd06000000')
- >>> pickle.dumps(oid, protocol=2)
- b'\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...'
- Python 2.7.15 (default, Jun 21 2018, 15:00:48)
- [GCC 7.3.0] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import pickle
- >>> pickle.loads('\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...')