Docs 菜单

Docs 主页开发应用程序MongoDB 驱动程序

pymongo

在此页面上

  • 简介
  • 安装
  • 连接到 MongoDB Atlas
  • 在没有 Stable API 的情况下连接到 MongoDB Atlas
  • 从 AWS Lambda 连接到 MongoDB Atlas
  • 连接到本地计算机上的 MongoDB Server
  • 兼容性

欢迎访问 PyMongo 文档站点,PyMongo 是同步 Python 应用程序的官方 MongoDB 驱动程序。使用 pip 下载 或者按照我们的教程设置可运行的项目。

提示

如果需要以非阻塞方式或从协程访问 MongoDB,我们建议改用 Motor 驱动程序。

您可以使用 PyMongo 驱动程序连接到以下环境中托管的部署:

MongoDB University Python 课程横幅广告

将 MongoDB 与 Python 结合使用

了解使用 MongoDB 进行 Python 应用程序开发的基础知识。

您必须安装 PyMongo 驱动程序模块才能使其可供 Python 应用程序使用。我们建议使用 pip 安装 PyMongo。

PyMongo 需要 dnsPython 以支持mongodb+srv:// 连接字符串和 MongoDB Atlas dnspython。安装或升级到最新 PyMongo 版本时会自动安装 。

以下命令演示了如何使用命令行安装最新版本的模块:

$ python -m pip install pymongo

如要安装特定版本的 PyMongo,请在命令中指定版本。以下命令显示如何使用 pip 安装 PyMongo 版本 3.11

$ python -m pip install pymongo==3.11

如果已安装 PyMongo 并需要升级到最新版本,请使用以下 pip 命令:

$ python -m pip install --upgrade pymongo

请参阅 “安装” 了解安装 PyMongo 的更多方法。

您可以使用以下连接片段来测试与 Atlas 上 MongoDB 部署的连接:

from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Set the Stable API version when creating a new client
client = MongoClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)

此连接代码段使用 Stable API 功能,您可以在使用 PyMongo 驱动程序 v3.12 (或更高版本)连接到 MongoDB Server v5.0 (或更高版本)时启用此功能。使用此功能时,您可以放心更新驱动程序或服务器,无需担心任何 Stable API 所支持的命令的向后兼容性问题。

要了解有关 Stable API 功能的更多信息,请参阅服务器手册中的Stable API

注意

从 2022 年 2 月开始,版本化 API被称为 Stable API。此次命名更改后,所有概念和功能均保持不变。

如果您使用的 MongoDB 版本或驱动程序不支持“稳定 API”功能,您可以使用以下代码片段来测试与 Atlas 上的 MongoDB 部署的连接:

from pymongo.mongo_client import MongoClient
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Create a new client and connect to the server
client = MongoClient(uri)
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)

要了解如何从 AWS Lambda 连接到 Atlas,请参阅使用 AWS Lambda 管理连接文档。

如果您需在本地机器上运行 MongoDB 服务器以用于开发目的而不是使用 Atlas 集群,则需完成以下操作:

  1. 下载CommunityEnterprise版本的 MongoDB Server。

  2. 安装和配置MongoDB Server。

  3. 启动该服务器。

重要

始终保护您的 MongoDB 服务器免受恶意攻击。请参阅我们的安全检查清单以获取安全建议列表。

在成功启动 MongoDB 服务器后,在驱动程序连接代码中指定连接字符串。

如果 MongoDB Server 在本地运行,您可以使用连接字符串 "mongodb://localhost:<port>",其中 <port> 是您配置服务器以侦听传入连接的端口号。

如果需要指定不同的主机名或 IP 地址,请参阅我们的《服务器手册》中有关连接字符串的条目。

要测试是否可以连接到服务器,请替换“连接到 MongoDB Atlas”代码示例中的连接字符串并运行。

重要

受支持的 PyMongo 和 Python 版本

MongoDB 不支持任何早于 v4.0 的 Python 驱动程序版本,以及任何早于 v3.6 的 Python 版本。

以下兼容性表规定了可以与指定版本 MongoDB 搭配使用的 MongoDB Python 驱动程序的推荐版本。

第一列列出驱动程序版本。

重要

在服务器版本生命周期结束 (EOL) 日期后的三年内,MongoDB 确保 MongoDB Server 和驱动程序之间的兼容性。要了解有关 MongoDB 版本和 EOL 日期的更多信息,请参阅MongoDB 软件生命周期时间表。

图标
解释
支持所有功能。
该驱动程序版本将与 MongoDB 版本一起使用,但并不支持所有新的 MongoDB 功能。
无标记
驱动程序版本未使用 MongoDB 版本测试。
PyMongo 驱动程序版本
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
4.7
4.6
4.5
4.4
4.3
4.2
4.1
4.0
3.13
3.12
3.11
3.10
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7

此驱动程序不支持早期版本的 MongoDB。

以下兼容性表规定了可以与指定版本 Python 搭配使用的 MongoDB Python 驱动程序的推荐版本。

第一列列出驱动程序版本。

PyMongo 驱动程序版本
Python 3.12
Python 3.11
Python 3.10 [1]
Python 3.9
Python 3.8
Python 3.7
Python 3.6
Python 3.5
Python 3.4
Python 3.3
PyPy3
Python 3.2
Python 3.1
4.7
4.6
4.5
4.4
4.3
4.2
4.1
[2]
4.0
3.13
3.12
3.11
3.10
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7
[1] Python3 104的版本。 及更高版本与 MongoDB 版本的 TLS/SSL0 不兼容。 及更早版本。请参阅 PyMongo 文档 以了解更多信息。
[2] Pymongo 4.1 需要 Python 3.6.2 或更高版本。

注意

PyPy3 是与 Python 3.2 兼容的替代解释器。

PyMongo 驱动程序版本
Python 2.7、PyPy
Python 2.6
Python 2.5、Jython 2.5
Python 2.4
4.0 [3]
3.12
3.11
3.10
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7
[3] PyMongo 4.0 及更高版本与 Python 2 不兼容

注意

Jython 2.5 是与 Python 2.5 兼容的替代解释器。

PyPy 是一种与 Python 2.7 兼容的替代解释器。

有关如何阅读兼容性表的更多信息,请参阅我们的 MongoDB 兼容性表指南。

← MongoDB Python 驱动程序