PyMongo Documentation
MongoDB's official guide to PyMongo, the Python driver, covering connections, CRUD, aggregation, indexes, transactions and the async API that has replaced Motor. Short, practical pages with runnable examples, and the right starting point for any Python backend, script or data pipeline on MongoDB.
4.4k (mongo-python-driver)
GitHub · Sept 2026
4.18.2 (September 2026)
PyPI · Sept 2026
63.9M
pypistats.org · Sept 2026
3.9 or later
PyPI · Sept 2026
Guide
Web Development
MongoDB
A weekend for the core, then reference
Intermediate
Free
What these docs cover
PyMongo is the official MongoDB driver for Python, and these are MongoDB's docs for its current 4.x line. The main sections are Get Started, connecting to MongoDB, CRUD operations, aggregation, indexes, security and authentication, monitoring and logging, and third-party integrations, with release notes, a compatibility table and the full API reference on Read the Docs.
Sync and async in one package
The most important recent change: PyMongo now includes an async API, AsyncMongoClient, built directly on asyncio. MongoDB deprecated Motor, the older async driver, on 14 May 2025 in favour of this API, and gave Motor an end-of-life date of 14 May 2026. The docs include a migration guide, which for most applications amounts to replacing MotorClient with AsyncMongoClient and changing imports. If you are starting a FastAPI project today, use PyMongo's async API rather than Motor, and be wary of tutorials that still recommend Motor.
Is it worth reading?
Yes, because Python makes MongoDB feel deceptively easy. Documents are dictionaries, so everything "just works" until you compare a string with an ObjectId, open a new client in every request, or store a float where you needed Decimal128 for money. The docs cover each of these properly, with short examples you can paste into a REPL.
What to read first
- Get Started, then the connection pages: one
MongoClientper process, reused, with the connection string in an environment variable. - CRUD:
insert_one,findwith filters and projections,update_onewith operators,find_one_and_update, and bulk writes. - Aggregation, using the same stages you practised in mongosh.
- Indexes, including how to create them from code at start-up.
- The async pages and the Motor migration guide, if you use
asyncio.
PyMongo does not validate your documents. Pair it with Pydantic models, or with JSON Schema validation on the collection, to keep shapes consistent.
Prerequisites
Python 3.9 or later (the current release's requirement on PyPI), comfort with dictionaries and basic MongoDB queries in the shell. For the async API, familiarity with async and await.
Common questions
PyMongo or an ODM such as MongoEngine or Beanie? Learn PyMongo first. ODMs sit on top of it, and understanding the driver makes their behaviour predictable.
Is it free? Yes, the driver is open source under Apache 2.0.
What can I do after it? Build Flask, Django or FastAPI backends, scripts and data pipelines on MongoDB with pooled connections, correct types and aggregation queries.
Best for: Python developers, from backend engineers to data scientists, who need to read and write MongoDB from code.
Ready to start?
Opens on MongoDB in a new tab.
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
Follow along
New writing, resources and project ideas land here first.