Motor
Asyncio MongoDB driver for Python offering non-blocking access to collections and cursors.
Official Documentation ↗motor 3.4+ / MongoDB 4.0+ / Python 3.9+(基于 asyncio). Check the official docs for the latest API surface.Installation
pip install motorMethods
No methods match this filter.
Overview
Motor provides async access to MongoDB for asyncio applications. It exposes the same document model as PyMongo but with awaitable methods that keep the event loop free during network waits.
Core Methods
Create a client, select a database and collection, then await insert_one, find_one, update_one and find. Cursors support async iteration or to_list for materialization.
Usage Notes
Share one client across the app, index query fields, and cap results with limit and projection. Use sessions for multi-document transactions on supported deployments.
Common Pitfalls
Calling blocking PyMongo code inside async functions stalls the loop. Unbounded cursor materialization exhausts memory, so iterate or limit. Forgetting to await a Motor call silently returns a coroutine that never runs.
FAQ
- How does Motor differ from PyMongo?
- Motor wraps PyMongo with async methods so operations do not block the event loop. Collections expose coroutine versions of insert, find and update.
- Should the client be reused?
- Yes. Create one AsyncIOMotorClient per process and reuse it; it manages pooling. Creating a client per request wastes connections.
- How do I use transactions?
- Start an async session with client.start_session and use async with session.start_transaction. Transactions require a replica set or sharded cluster.
Report a problem with this API docOpens a pre-filled issue on GitHub.