mongodb-asyncmongoclient-patterns
Skill4 starsChanged 5 months agoMITView on GitHub
PyMongo 4.13+ AsyncMongoClient patterns (the official Motor replacement; Motor deprecated 2026-05-14) — async cursors with `async for`, AsyncCollection repository pattern, JSON-driven idempotent index management, Pydantic v2 + AwareDatetime documents, replica-set transactions via `with_transaction`. Use for async MongoDB CRUD layers in FastAPI services. Sibling to `mongodb-atlas-patterns` (sync + Atlas Search).
In this repository101 other files it gives its agents.
- ai-video-understandingSkill
- apple-foundation-modelsSkill
- arch-dddSkill
- arch-decision-recordsSkill
- arch-diagramsSkill
- arch-eventsSkill
- arch-mvp-roadmapSkill
- arch-python-modernSkill
- aws-app-runnerSkill
- aws-cicd-patternsSkill
- aws-security-hardeningSkill
- azure-functions-python-v2Skill
- azure-identity-m2m-authSkill
- azure-service-bus-messagingSkill
- azure-workflow-execution-modelsSkill
- caddy-tls-proxySkill
- compliance-frameworksSkill
- design-system-workflowSkill
- dev-debugging-strategiesSkill
- dev-standardsSkill
- docker-python-poetrySkill
- docker-pythonSkill
- fastapi-patternsSkill
- ffmpeg-laravel-videoSkill
- formik-yup-react-nativeSkill
- frontend-accessibilitySkill
- frontend-angular-aiSkill
- frontend-angularSkill
- frontend-angular-toolingSkill
- frontend-design-systemsSkill
- frontend-material-chatSkill
- frontend-material-design-3Skill
- frontend-svelteSkill
- gcp-cicd-patternsSkill
- gcp-cloud-runSkill
- gcp-cloudsql-infrastructureSkill
- gcp-finopsSkill
- gcp-observabilitySkill
- gcp-security-hardeningSkill
- github-actions-cicdSkill
- healthkit-biofeedbackSkill
- infra-terraformSkill
- ios-app-qualitySkill
- ios-audio-hapticsSkill
- jest-nestjs-testingSkill
- langchain-azure-openai-patternsSkill
- laravel-auth-hardeningSkill
- laravel-ci-mysqlSkill
- laravel-mix-webpackSkill
- laravel-modern-patternsSkill
- laravel-pint-patternsSkill
- laravel-s3-storageSkill
- laravel-sail-dockerSkill
- mobile-app-deploymentSkill
- mobile-e2e-testingSkill
- mobile-offline-syncSkill
- mobile-push-notificationsSkill
- mobile-video-uploadSkill
- mongodb-atlas-patternsSkill
- nestjs-patternsSkill
- The first 60 shown.
The file
Files
MongoDB AsyncMongoClient Patterns
This is the async sibling to mongodb-atlas-patterns (sync). Motor is deprecated 2026-05-14 — use pymongo.AsyncMongoClient (PyMongo 4.9+, stable 4.13+). Pool sizing, index design, aggregation rules, Atlas SRV — all unchanged from the sync skill; this one covers only what differs in async.
When to use
- Async FastAPI services hitting MongoDB (CRUD layer, not search)
- Migrating off Motor before the 2026-05-14 deprecation
- Replica-set / Atlas transactions with
with_transactionretry - Pydantic v2 +
AwareDatetimeround-tripping through BSON - JSON-driven idempotent index management at startup
For sync PyMongo, Atlas Search, $search pipelines → see mongodb-atlas-patterns. For Pydantic v2 idioms → arch-python-modern.
Core patterns
| Pattern | One-line rule |
| Import | from pymongo import AsyncMongoClient (not motor.motor_asyncio) |
| Single event loop | AsyncMongoClient is not thread-safe — one client per event loop, never share across threads |
| Singleton | One client per process via FastAPI lifespan + app.state.mongo_client; never per-request |
| Async cursor | async for doc in collection.find(...) for streaming |
| Bounded list | await cursor.to_list(length=N) — explicit length |
| Unbounded list | await cursor.to_list(None) — Motor's to_list(0) is invalid in PyMongo async |
await everywhere | All collection ops are awaitable: find_one, insert_one, update_one, index_information, create_index, server_info, command |
| Repository pattern | Inject AsyncMongoClient into a base AsyncMongoDBConnection; per-collection CRUD subclasses set collection_name and lazy-init the AsyncCollection |
tz_aware codec | CodecOptions(tz_aware=True) on every collection handle so BSON datetimes decode with UTC tzinfo (required for AwareDatetime) |
| JSON-driven indexes | Define indexes in mongo_collection_config/{coll}.json; set_collection_indexes() runs at startup, idempotent via index_information() |
| Transactions | async with client.start_session() as s: await s.with_transaction(callback) — auto-retries transient errors; callback must be idempotent |
| Task cancellation | Cancelling an asyncio task running PyMongo is a fatal interrupt — never reuse cursors/sessions after cancel |
Quick reference
from contextlib import asynccontextmanager
from pymongo import AsyncMongoClient
from pymongo.asynchronous.collection import AsyncCollection
from bson import CodecOptions
@asynccontextmanager
async def lifespan(app):
app.state.mongo = AsyncMongoClient(
os.environ["MONGODB_URI"], # mongodb+srv://...
maxPoolSize=100, minPoolSize=5, # async drives more concurrency than sync
maxIdleTimeMS=30_000,
serverSelectionTimeoutMS=30_000,
retryWrites=True, retryReads=True,
)
await app.state.mongo[db_name].command("ping") # eager verify
yield
await app.state.mongo.close()
class TransactionCRUD:
def __init__(self, client: AsyncMongoClient, db: str, coll: str):
self.coll: AsyncCollection = client.get_database(db).get_collection(
coll, codec_options=CodecOptions(tz_aware=True),
)
async def get(self, _id: str) -> dict | None:
return await self.coll.find_one({"_id": _id})
async def list(self, limit: int = 1000) -> list[dict]:
return await self.coll.find().to_list(length=limit)
async def stream(self):
async for doc in self.coll.find({"status": "active"}):
yield docAnti-patterns (top 5)
from motor.motor_asyncio import AsyncIOMotorClient→ deprecated 2026-05-14; switch tofrom pymongo import AsyncMongoClient- Missing
awaiton async methods (e.g.,self.client.server_info()in a health-check) → silently returns a coroutine, evaluates truthy, hides outages. Alwaysawait— see project'scheck_databasebug in examples.md await cursor.to_list(0)→ invalid in PyMongo async (Motor idiom). Useto_list(None)for unbounded orto_list(length=N)- Sync
MongoClientmixed into async code → blocks event loop. UseAsyncMongoClientend-to-end; for sync legacy bridge wrap withasyncio.to_thread(seemongodb-atlas-patterns) - Returning raw BSON
dictfrom a repository → no validation, no type safety. Validate at the boundary:return TransactionRead(**document)
Migration cheat sheet, full Motor → PyMongo Async API mapping, Pydantic v2 + BSON round-tripping, and transaction details live in reference.md. Project-grounded code (including the latent check_database missing-await bug + fix) lives in examples.md.
See also
mongodb-atlas-patterns— sync sibling; Atlas Search,$searchpipelines, pool sizing, write concerns (all foundations transfer unchanged)arch-python-modern— Pydantic v2 validators,field_validatorvsmodel_validator, modern async idiomsfastapi-patterns— lifespan, dependency injection, app.state singletons
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

