Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Pymongo ドライバー
/

に移行します

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

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

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

# Motor client import
from motor.motor_asyncio import AsyncIOMotorClient
# ​ client import
from pymongo import AsyncMongoClient

ドライバーで使用できる非同期メソッドのリストを確認するには、 PyMongo toガイドの「非同期メソッド」セクションを参照してください。

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

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

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

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

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

  • AsyncCursor.to_list(0) は、 ドライバー では有効ではありません。代わりに to_list(None) を使用してください。

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

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

  • MongoClient のすべての使用を AsyncMongoClient に置き換えます。

  • すべての非同期メソッド呼び出しに await キーワードを追加します。

  • 関数内で非同期メソッドを呼び出す場合は、その関数を async としてマークします。

次のセクションでは、非同期APIを実装する方法について説明します。

次の表は、 ドライバーで使用できる非同期メソッドを示しています。これらのメソッドを呼び出すには、それらを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 ドキュメントを参照してください。

戻る

アップグレード ガイド

項目一覧