Motor (Async MongoDB)
DEPRECATED as of May 14, 2025. Motor was the official async Python driver for MongoDB. Superseded by the native AsyncMongoClient in pymongo >= 4.13 (GA). EOL: May 14, 2026. Critical bug fixes only until May 14, 2027. No new features. Current version: 3.7.1. Requires PyMongo 4.9+. For new projects use pymongo's AsyncMongoClient instead.
Warnings
- breaking Motor deprecated May 14, 2025. EOL May 14, 2026. No new features. Bug fixes only. Migrate to pymongo AsyncMongoClient.
- breaking Motor < 3.6 is incompatible with PyMongo >= 4.9. Version mismatch causes ImportError or unexpected failures.
- gotcha Import path is motor.motor_asyncio.AsyncIOMotorClient — not motor.AsyncIOMotorClient. LLMs frequently drop the motor_asyncio submodule.
- gotcha Motor does NOT use true async I/O — it uses a thread pool under the hood. In high-concurrency scenarios this can cause performance degradation. pymongo's AsyncMongoClient uses true asyncio.
- gotcha Motor cursor iteration requires 'async for' not 'for'. Sync iteration over MotorCursor silently returns zero results instead of raising an error.
- gotcha Python 3.8 dropped in motor 3.7. Python 3.9 dropped in motor 3.8. Check compatibility matrix before upgrading.
Install
-
pip install pymongo -
pip install motor
Imports
- AsyncMongoClient (recommended — pymongo native)
from pymongo import AsyncMongoClient import asyncio async def main(): client = AsyncMongoClient('mongodb+srv://user:pass@cluster.mongodb.net/') db = client['mydb'] result = await db['users'].insert_one({'name': 'Alice'}) print(result.inserted_id) await client.close() asyncio.run(main()) - AsyncIOMotorClient (motor — existing code)
import motor.motor_asyncio import asyncio async def main(): client = motor.motor_asyncio.AsyncIOMotorClient( 'mongodb+srv://user:pass@cluster.mongodb.net/' ) db = client['mydb'] result = await db['users'].insert_one({'name': 'Alice'}) print(result.inserted_id) asyncio.run(main())
Quickstart
# New projects: use pymongo's native async instead of motor
# pip install 'pymongo[srv]'
from pymongo import AsyncMongoClient
import asyncio
async def main():
client = AsyncMongoClient('mongodb+srv://user:pass@cluster.mongodb.net/')
db = client['mydb']
col = db['users']
# All same methods as sync pymongo but with await
await col.insert_one({'name': 'Alice', 'age': 30})
doc = await col.find_one({'name': 'Alice'})
print(doc)
await col.update_one({'name': 'Alice'}, {'$set': {'age': 31}})
count = await col.count_documents({})
print(count)
await client.close()
asyncio.run(main())