Docs 菜单
Docs 主页
/
MongoDB 驱动程序
/

Motor(异步驱动程序)

在此页面上

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

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

提示

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

请访问以下链接阅读博客文章,这些文章描述了 Motor 驱动程序的具体用例:

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

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

$ python -m pip install motor

有关要求和其他安装方法的更多信息,请参阅 Motor Installation 文档。

您可以使用以下连接片段,通过 asyncio 异步框架来测试与 Atlas 上 MongoDB 部署的连接:

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

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

如需了解有关“Stable API”功能的更多信息,请参阅“服务器”手册中的“Stable API”。

注意

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

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

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

如果您使用的是 tornado 异步库,则可使用以下代码连接到 MongoDB 部署:

import tornado
import motor
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Set a 5-second connection timeout when creating a new client
client = motor.motor_tornado.MotorClient(uri, serverSelectionTimeoutMS=5000)
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
tornado.ioloop.IOLoop.current().run_sync(ping_server)

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

  1. 下载CommunityEnterprise版本的 MongoDB Server。

  2. 安装并配置 MongoDB Server。

  3. 启动该服务器。

重要

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

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

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

如果您需要指定不同的主机名或 IP 地址,请参阅 Server 手册中有关连接字符串的条目。

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

以下兼容性表指定了可以与特定版本 MongoDB 配合使用的 Motor ( Python 异步)驱动程序的推荐版本。

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

重要

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

圖標
解释
支持所有功能。
该驱动程序版本将与 MongoDB 版本一起使用,但并不支持所有新的 MongoDB 功能。
无标记
驱动程序版本未使用 MongoDB 版本测试。
Motor(Python 异步)驱动程序版本
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
3.2 到 3.5
3.1
3.0

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

以下兼容性表指定了可以与特定版本 Python 搭配使用的 Motor ( Python 异步)驱动程序的推荐版本。

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

Motor(Python 异步)驱动程序版本
Python 3.12
Python 3.11
Python 3.10
Python 3.9
Python 3.8
Python 3.7
3.5
3.3 到 3.4
3.1 至 3.2
3.0
  • 电机3 。 5包装 PyMongo 4 。将5更改为4 。 8

  • Motor 3.3 和 3.4 封装 PyMongo 4.5

  • Motor 3.2 会封装 PyMongo 4.4 及更高版本

  • Motor 3.1 会封装 PyMongo 4.2 及更高版本

  • Motor 3.0 会封装 PyMongo 4.1 及更高版本

注意

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

后退

Python 驱动程序

来年

Ruby 驱动程序