Using the MongoDB client

The MongoDB clients made available in the context are objects compatible with the AsyncIOMotorClient API. As such, refer to Motor’s asyncio tutorial and api documentation for details.

An example demonstrating the use of the client in Asphalt:

from asphalt.core import inject, resource

@inject
async def handler(*, mongodb: AsyncIOMotorClient = resource() -> None:
    # Insert a document to somecollection in somedb
    collection = mongodb.somedb.somecollection
    document = {'key': 'value'}
    obj_id = await collection.insert(document)

    # Delete the document
    await collection.remove(obj_id)