Python 3常见问题解答#
支持哪些Python3版本?#
PyMongo支持CPython3.7+和PYP3.8+。
python3的PyMongo行为有什么变化吗?#
只有一个故意的改变。实例 bytes
编码为BSON类型5(二进制数据),子类型为0。在python3中,它们被解码回 bytes
. 在python2中,它们被解码为 Binary
子类型为0。
例如,让我们插入一个 bytes
实例使用python3,然后读回它。注意字节字符串被解码回 bytes
::
Python 3.7.9 (v3.7.9:13c94747c7, Aug 15 2020, 01:31:08)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> c = pymongo.MongoClient()
>>> c.test.bintest.insert_one({'binary': b'this is a byte string'}).inserted_id
ObjectId('4f9086b1fba5222021000000')
>>> c.test.bintest.find_one()
{'binary': b'this is a byte string', '_id': ObjectId('4f9086b1fba5222021000000')}
现在在python2中检索相同的文档。请注意,字节字符串被解码为 Binary
::
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')}
在解析子类型为0的JSON二进制文件时,行为也有类似的变化。在python3中,它们被解码成 bytes
. 在python2中,它们被解码为 Binary
子类型为0。
例如,让我们使用python3解码JSON二进制子类型0。请注意,字节字符串被解码为 bytes
::
Python 3.7.9 (v3.7.9:13c94747c7, Aug 15 2020, 01:31:08)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bson.json_util import loads
>>> loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}')
{'b': b'this is a byte string'}
现在在python2中解码相同的JSON。请注意,字节字符串被解码为 Binary
::
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[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)}