文档菜单
文档首页
/ / /
PyMongo

从PyMongo切换到PyMongo异步

本页内容

  • 概述
  • 从PyMongo切换
  • 异步方法
  • 更多信息

重要

PyMongo异步驱动是实验性的,不应在生产环境中使用。本指南中描述的类、方法和行为可能在完整发布之前发生变化。如果您在使用PyMongo异步时遇到任何问题,您可以在问题与帮助页面学习如何报告它们。问题与帮助

PyMongo 异步驱动程序是 PyMongo 和Motor 库的统一。在本指南中,您可以识别从 PyMongo 切换到 PyMongo 异步必须做出的更改。

PyMongo 异步驱动程序的行为类似于 PyMongo,但所有执行网络操作的方法都是协程,必须使用 await 等待。要从 PyMongo 切换到 PyMongo 异步,您必须按照以下方式更新您的代码

  • 将所有使用MongoClient的实例替换为AsyncMongoClient

  • 将所有异步方法调用添加await关键字。

  • 如果在一个函数内部调用异步方法,请将此函数标记为async

以下部分描述了如何实现异步 API。

以下表格列出了PyMongo Async驱动程序中可用的异步方法。要调用这些方法,您必须使用await并在一个async函数中调用它们。

方法
示例
AsyncMongoClient()
from pymongo import AsyncMongoClient
async with AsyncMongoClient(...)
watch()
async with await client.watch(...) as stream:
...
server_info()
await client.server_info(...)
list_databases()
await client.list_databases()
list_database_names()
await client.list_database_names()
drop_database()
await client.drop_database(...)
方法
示例
watch()
async with await db.watch(...) as stream:
...
create_collection()
await db.create_collection(...)
aggregate()
async with await client.admin.aggregate(...) as cursor:
...
command()
await db.command(...)
cursor_command()
await db.cursor_command(...)
list_collections()
await db.list_collections()
list_collection_names()
await db.list_collection_names()
drop_collection()
await db.drop_collection(...)
validate_collection()
await db.validate_collection(...)
dereference()
await db.dereference(...)
方法
示例
watch()
async with await collection.watch(...) as stream:
...
insert_one()
await collection.insert_one(...)
insert_many()
await collection.insert_many(...)
replace_one()
await collection.replace_one(...)
update_one()
await collection.update_one(...)
update_many()
await collection.update_many(...)
drop()
await collection.drop()
delete_one()
await collection.delete_one(...)
delete_many()
await collection.delete_many(...)
find_one()
await collection.find_one(...)
estimated_document_count()
await collection.estimated_document_count()
count_documents()
await collection.count_documents(...)
create_index()
await collection.create_index(...)
create_indexes()
await collection.create_indexes(...)
drop_index()
await collection.drop_index(...)
drop_indexes()
await collection.drop_indexes()
list_indexes()
await collection.list_indexes()
index_information()
await collection.index_information()
list_search_indexes()
await collection.list_search_indexes()
create_search_index()
await collection.create_search_index(...)
create_search_indexes()
await collection.create_search_indexes(...)
drop_search_index()
await collection.drop_search_index(...)
update_search_index()
await collection.update_search_index(...)
options()
await collection.options()
aggregate()
async for doc in await collection.aggregate(...):
...
aggregate_raw_batches()
async for batch in await collection.aggregate_raw_batches(...):
...
rename()
await collection.rename(...)
distinct()
await collection.distinct(...)
find_one_and_delete()
await collection.find_one_and_delete(...)
find_one_and_replace()
await collection.find_one_and_replace(...)
find_one_and_update()
await collection.find_one_and_update(...)

要了解更多关于异步Python的信息,请参阅Python Asyncio文档。

返回

从Motor迁移