Motor Async MongoDB Aggregation Pipeline

Run aggregation pipelines with the async Motor driver, streaming large result sets via async iteration and cursor batching.

PythonData Fetching

Code

pythonsnippet
62 lines
import asyncio

from motor.motor_asyncio import AsyncIOMotorClient

client = AsyncIOMotorClient('mongodb://localhost:27017')
orders = client['shop']['orders']


PIPELINE = [
    {'$match': {'status': 'paid'}},
    {'$unwind': '$items'},
    {'$group': {
        '_id': '$items.sku',
        'units': {'$sum': '$items.qty'},
        'revenue': {'$sum': {'$multiply': ['$items.qty', '$items.price']}},
    }},
    {'$sort': {'revenue': -1}},
    {'$limit': 20},
]


async def top_products() -> list[dict]:
    cursor = orders.aggregate(
        PIPELINE,
        batchSize=200,
        allowDiskUse=True,
    )

    results: list[dict] = []
    async for doc in cursor:
        results.append({
            'sku': doc['_id'],
            'units': doc['units'],
            'revenue': round(doc['revenue'], 2),
        })
    return results


async def seed() -> None:
    await orders.delete_many({})
    await orders.insert_many([
        {'status': 'paid', 'items': [
            {'sku': 'A-1', 'qty': 2, 'price': 10.0},
            {'sku': 'B-2', 'qty': 1, 'price': 30.0},
        ]},
        {'status': 'paid', 'items': [
            {'sku': 'A-1', 'qty': 5, 'price': 10.0},
        ]},
        {'status': 'refunded', 'items': [
            {'sku': 'C-3', 'qty': 1, 'price': 99.0},
        ]},
    ])


async def main() -> None:
    await seed()
    for row in await top_products():
        print(row)
    client.close()


asyncio.run(main())

What the code does

Count the functions top_products, seed and main as the entire surface area of Motor Async MongoDB Aggregation Pipeline.

One loop does the cycling here: for row in await top_products(). Asynchronous boundaries use await, so failures surface as exceptions at the call site rather than being swallowed. AsyncIOMotorClient are the only symbols borrowed from motor — the coupling is explicit and narrow.

Watch out for

  • Plan for an install step: motor and asyncio are a real dependency, resolved by the package manager.

Applying it

The main signature reads def top_products() -> list[dict]

top_products is the public entry point; everything else in the file exists to support it.

Statements that matter

async def main() -> None:
    await seed()
    for row in await top_products():
        print(row)
    client.close()
await orders.insert_many([
{'status': 'paid', 'items': [
{'sku': 'A-1', 'qty': 2, 'price': 10.0},
{'sku': 'B-2', 'qty': 1, 'price': 30.0},
]},
{'status': 'paid', 'items': [
{'sku': 'A-1', 'qty': 5, 'price': 10.0},
]},
{'status': 'refunded', 'items': [
{'sku': 'C-3', 'qty': 1, 'price': 99.0},
]},
])

FAQ

Do I need to install anything before running this?
Yes: `motor` and `asyncio` must be present in the project or the import fails at load time.