兼容性
MongoDB 兼容性
以下兼容性表指定了与特定版本的 MongoDB 一起使用的推荐版本或 PyMongo 版本。
第一列列出驱动程序版本。
重要
在服务器版本生命周期结束 (EOL) 日期之后的三年内,MongoDB 将确保 MongoDB Server 与驱动程序兼容。要了解有关 MongoDB 版本和 EOL 日期的更多信息,请参阅 MongoDB 软件生命周期时间表。。
兼容性表图例
Icon | 解释 |
---|---|
✓ | 支持所有功能。 |
⊛ | 驾驶员版本将与MongoDB版本一起使用,但并不支持所有新的MongoDB功能。 |
✗ | 该驾驶员版本不适用于MongoDB版本。尝试连接到MongoDB版本将导致错误。 |
无标记 | 该驾驶员版本未使用MongoDB版本进行测试。 |
PyMongo 版本 | MongoDB 8.0 | MongoDB 7.0 | MongoDB 6.0 | MongoDB 5.0 | MongoDB 4.4 | MongoDB 4.2 | MongoDB 4.0 | MongoDB 3.6 |
---|---|---|---|---|---|---|---|---|
4.11 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
4.9 到 4.10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.4 到 4.8 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.2 到 4.3 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.12 到 4.1 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.11 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ |
3.9 到 3.10 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ |
3.7 到 3.8 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ |
语言兼容性
PyMongo 同时支持 CPython 和 PyPy。
当 Python 的一个版本被标记为生命周期结束 (EOL) 时,PyMongo 的下一个次要版本将删除对该版本的支持。 该驱动程序为 EOL Python 版本提供以下持续支持:
CPython :与 EOL CPython 版本兼容的最后一个 PyMongo 小版本获得了为期一年的重大错误修复。
PyPy :PyMongo 不支持超过 EOL 日期的 PyPy 版本。
以下兼容性表指定了与特定版本的 Python 一起使用的推荐版本 PyMongo。 第一列列出了驱动程序版本。
Python 3
以下兼容性表显示了 PyMongo 与不同版本的 CPython 和 PyPy 的兼容性。
有关如何读取兼容性表的更多信息,请参阅MongoDB兼容性表。
CPython
PyPy
PyMongo 版本 | PyPy3.10 | PyPy3.9 | PyPy3.8 | PyPy3.7 | PyPy3.6 | PyPy3.5 |
---|---|---|---|---|---|---|
4.11 到 4.12 | ✓ | |||||
4.8 到 4.10 | ✓ | ✓ | ||||
4.5 到 4.7 | ✓ | ✓ | ✓ | |||
4.2 到 4.4 | ✓ | ✓ | ✓ | ✓ | ||
4.1 [ 2 ] | ✓ | ✓ | ✓ | ✓ | ✓ | |
4.0 | ✓ | ✓ | ✓ | ✓ | ✓ | |
3.12 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.11 | ✓ | ✓ | ✓ | ✓ | ✓ | |
3.10 | ✓ | ✓ | ✓ | ✓ | ||
3.7 到 3.9 | ✓ | ✓ | ✓ |
[1] | Python 3.10及更高版本与MongoDB 4.0及更早版本的 TLS/SSL 不兼容。 有关详细信息,请参阅故障排除指南的 TLS部分。 |
[2] | (1、2) PyMongo 4.1 需要Python 3.6.2 或更高版本。 |
Python 2
PyMongo版本 3.7 到 3.12 与Python 2.7 和 PyPy2.7 兼容。但是,在某些情况下, PyMongo应用程序在Python 2 环境中运行时的行为会有所不同。
以下部分描述了使用PyMongo时Python 2 和Python 3 之间的行为差异。
二进制数据
在所有版本的Python中, PyMongo将字节类的实例编码为具有子类型 0(二进制数据的默认子类型)的二进制数据。在Python 3 中, PyMongo将这些值解码为 bytes
类的实例。在Python 2 中,驾驶员将它们解码为具有子类型 0 的Binary类的实例。
以下代码示例展示了PyMongo如何解码 bytes
类的实例。选择 Python 2 或 Python 3标签页以查看相应的代码。
from pymongo import MongoClient client = MongoClient() client.test.test.insert_one({'binary': b'this is a byte string'}) doc = client.test.test.find_one() print(doc)
{u'_id': ObjectId('67afb78298f604a28f0247b4'), u'binary': Binary('this is a byte string', 0)}
from pymongo import MongoClient client = MongoClient() client.test.test.insert_one({'binary': b'this is a byte string'}) doc = client.test.test.find_one() print(doc)
{'_id': ObjectId('67afb78298f604a28f0247b4'), 'binary': b'this is a byte string'}
在解码子类型为 0 的JSON二进制值时,驾驶员的行为方式相同。在Python 3 中,它将这些值解码为 bytes
类的实例。在Python 2 中,驾驶员将它们解码为具有子类型 0 的 Binary
类的实例。有关显示差异的代码示例,请参阅扩展JSON页面。
Pickled ObjectIds
如果您在Python 2 中对 ObjectId
进行了 pickle,并想在Python 3 中对其进行 unpickle,则必须将 encoding='latin-1'
作为参数传递给 pickle.loads()
方法。
以下示例展示了如何使用Python 3 对在Python 2 中进行 pickle 的 ObjectId
进行 unpickle:
import pickle pickle.loads(b'<ObjectId byte stream>', encoding='latin-1')
如果Python 3应用程序使用兼容的序列化协议来 pickle ObjectId
,您可以使用Python 2 来取消 pickle。要在Python 3 中指定兼容协议,请为 pickle.dumps()
方法的 protocol
参数传递 0、1 或 2 的值。
以下示例在Python 3 中序列化 ObjectId
,然后打印 ObjectId
和生成的 bytes
实例:
import pickle from bson.objectid import ObjectId oid = ObjectId() oid_bytes = pickle.dumps(oid, protocol=2) print("ObjectId: {}".format(oid)) print("ObjectId bytes: {}".format(oid_bytes))
ObjectId: 67af9b1fae9260c0e97eb9eb ObjectId bytes: b'\x80\x02cbson.objectid\nObjectId\nq\x00...
以下示例取消上一示例中 ObjectId
的封装,然后打印 bytes
和 ObjectId
实例:
import pickle from bson.objectid import ObjectId oid_bytes = b'\x80\x02cbson.objectid\nObjectId\nq\x00...' oid = pickle.loads(oid_bytes) print("ObjectId bytes: {}".format(oid_bytes)) print("ObjectId: {}".format(oid))
ObjectId bytes: b'\x80\x02cbson.objectid\nObjectId\nq\x00)... ObjectId: 67af9b1fae9260c0e97eb9eb
Motor兼容性
下表显示了Motor和PyMongo的等效版本。
Motor版本 | PyMongo 版本 |
---|---|
3.7 | 4.10 |
3.6 | 4.9 |
3.3 | 4.4 |
3.1 | 4.2 |
3.0 | 4.0 |