Menu Docs

Mudar do PyMongo para o PyMongo Async

Importante

O driver PyMongo Async é experimental e não deve ser usado em ambientes de produção. Classes, métodos e comportamentos descritos neste guia podem mudar antes da versão completa. Se você encontrar algum problema com o PyMongo Async, saiba como denunciá-lo na página de ajuda.

O driver PyMongo Async é uma unificação do PyMongo e da biblioteca Motor. Neste guia, você pode identificar as alterações que deve fazer para alternar do PyMongo para o PyMongo Async.

O driver PyMongo Async se comporta de forma semelhante ao PyMongo, mas todos os métodos que executam operações de rede são corrotinas e devem ser aguardadas. Para alternar do PyMongo para o PyMongo Async, você deve atualizar seu código das seguintes maneiras:

  • Substitua todos os usos de MongoClient por AsyncMongoClient.

  • Adicione a palavra-chave await a todas as chamadas de método assíncrono.

  • Se você chamar um método assíncrono dentro de uma função, marque a função como async.

As seções a seguir descrevem como implementar a API assíncrona.

As tabelas a seguir listam os métodos assíncronos disponíveis no driver PyMongo Async. Para chamar esses métodos, você deve await e chamá-los dentro de uma função async.

Método
Exemplo

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(...)
Método
Exemplo

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(...)
Método
Exemplo

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

Para saber mais sobre Python assíncrono, consulte a documentação do Python Asyncio.