Docs 菜单
Docs 主页
/ / /
pymongo

从PyMongo切换到PyMongo Async

在此页面上

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

重要

PyMongo异步驾驶员是实验性的,不应在生产环境中使用。 在发布完整发布之前,本指南中描述的类、方法和行为可能会发生变化。 如果您在使用PyMongo Async 时遇到任何问题,可以在 问题和帮助页面上学习;了解如何报告这些问题。

PyMongo异步驾驶员是PyMongo和 Motor库的统一。在本指南中,您可以了解从PyMongo切换到PyMongo Async 时必须进行的更改。

PyMongo异步驾驶员的行为与PyMongo类似,但所有执行网络操作的方法都是协程,必须等待。 要从PyMongo切换到PyMongo Async,您必须通过以下方式更新代码:

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

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

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

以下部分介绍如何实现异步API。

下表列出了PyMongo异步驾驶员中可用的异步方法。 要调用这些方法,您必须对其进行 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迁移