ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

PyMongo Async への移行

重要

PyMongo Async ドライバーは実験的なものであり、本番環境では使用しないでください。 このガイドで説明されているクラス、メソッド、および動作は、完全なリリースより前に変更される可能性があります。 PyMongo Async で問題が発生した場合は、[ 問題とヘルプ ] ページでその問題を報告する方法について学びます。

PyMongo Async ドライバーは、 PyMongoとMotorライブラリの統合です。このガイドでは、アプリケーションをPyMongoまたはMotorからPyMongo Async ドライバーに移行するために行う必要がある変更を識別できます。

PyMongo Async ドライバーはMotorライブラリと同様に機能しますが、スレッド プールに作業を委任するのではなくPython Asyncio を直接使用するため、レイテンシとスループットの向上が可能です。 ほとんどの場合、MotorClient の代わりに AsyncMongoClient を使用し、アプリケーションのインポート ステートメントを pymongo からインポートするように変更することで、既存のMotorアプリケーションをPyMongo Async に直接移行できます。

次の例は、 Motorでの読み取りおよび書込み (write) 操作にクライアントを使用するためのインポートにおけるPyMongo Async との違いを示しています。

# Motor client import
from motor.motor_asyncio import AsyncIOMotorClient
# PyMongo Async client import
from pymongo import AsyncMongoClient

PyMongo Async ドライバーで使用できる非同期メソッドのリストを表示するには、 PyMongoからPyMongo Async へのガイドの非同期メソッドセクションを参照してください。

次のセクションでは、 MotorからPyMongo Async ドライバーに移行するときにアプリケーションに実装する必要があるメソッド署名の変更を示します。

PyMongo Async ドライバーでは、次のMotorメソッド署名の動作が異なります。

  • AsyncMongoClient.__init__()io_loop パラメータを受け入れません。

  • AsyncCursor.each() はPyMongo Async ドライバーに存在しません。

  • MotorGridOut.stream_to_handler() はPyMongo Async ドライバーに存在しません。

  • AsyncCursor.to_list(0) はPyMongo Async ドライバーでは無効です。代わりに to_list(None) を使用してください。

  • MongoClient はスレッドセーフであり、多くのスレッドで使用できますが、AsyncMongoClient はスレッドセーフではないため、単一のイベントループでのみ使用する必要があります。

PyMongo Async ドライバーはPyMongoと同様に動作しますが、ネットワーク操作を実行するすべてのメソッドはコルーチンであり、待機する必要があります。 PyMongoからPyMongo Async に移行するには、次の方法でコードを更新する必要があります。

  • 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 ドキュメントを参照してください。