文档菜单
文档首页
/
MongoDB 驱动程序
/

Motor (异步驱动程序)

本页内容

  • 简介
  • 安装
  • 连接到 MongoDB Atlas
  • 在不使用稳定 API 的情况下连接到 MongoDB Atlas
  • 连接到本地机器上的 MongoDB 服务器
  • 兼容性

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

提示

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

点击以下链接阅读描述Motor驱动程序特定用例的博客文章

您必须安装电机驱动模块,以便将其提供给您的Python应用程序。我们建议使用pip安装Motor。

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

$ python -m pip install motor

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

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

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())

此连接代码片段使用稳定API功能,您可以在使用Motor驱动程序v2.5及以上版本连接到MongoDB服务器v5.0及以上版本时启用此功能。使用此功能时,您可以更新驱动程序或服务器,无需担心与稳定API覆盖的任何命令的向后兼容性问题。

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

注意

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

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

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. 下载MongoDB服务器的社区版企业版

  2. 安装和配置 MongoDB服务器。

  3. 启动服务器。

重要

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

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

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

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

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

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

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

重要

MongoDB 保证在服务器版本的生命周期结束(EOL)日期后三年内 MongoDB 服务器与驱动程序的兼容性。有关 MongoDB 发布和 EOL 日期的更多信息,请参阅 MongoDB 软件生命周期计划

图标
说明
支持所有功能。
驱动程序版本将与 MongoDB 版本一起工作,但并不支持所有新的 MongoDB 功能。
无标记
驱动程序版本与MongoDB版本未进行测试。
电机驱动程序版本
MongoDB 8.0
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
3.6
3.2至3.5
3.1
3.0

驱动程序不支持MongoDB的旧版本。

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

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

电机驱动程序版本
Python 3.13
Python 3.12
Python 3.11
Python 3.10
Python 3.9
Python 3.8
Python 3.7
3.6
3.5
3.3至3.4
3.1至3.2
3.0
  • Motor 3.6包装PyMongo 4.9

  • Motor 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+

注意

  • 为了支持asyncio,Motor需要Python 3.4+,或Python 3.3与PyPI的asyncio包

  • Motor 2.3+支持Windows。

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

返回

Python 驱动程序