API Reference
PyMongo
The official synchronous Python driver for MongoDB, with collection operations, aggregation, and gridfs.
Official Documentation ↗CompatibilityThis documentation covers version
v4.6. Check the official docs for the latest API surface.Installation
pip
pip install pymongoMethods
No methods match this filter.
PyMongo talks to MongoDB over the wire protocol and exposes databases, collections, and cursors.
Key Features
- Document CRUD and bulk writes
- Aggregation pipelines and indexes
- Connection pooling and retryable writes
Usage Notes
Pin a version and check the official docs for the API surface you depend on before upgrading.
Errors
The official API reference declares the following error types:
pymongo.errors.PyMongoError(exception) — official text: “exception pymongo.errors.PyMongoError(message=‘’, error_labels=None) — Base class for all PyMongo exceptions.” (source: https://pymongo.readthedocs.io/en/stable/api/pymongo/errors.html#pymongo.errors.PyMongoError, fetched 2026-11-08T16:03:08.4363995+08:00)pymongo.errors.ConnectionFailure(exception) — official text: “exception pymongo.errors.ConnectionFailure(message=‘’, error_labels=None) — Raised when a connection to the database cannot be made or is lost.” (source: https://pymongo.readthedocs.io/en/stable/api/pymongo/errors.html#pymongo.errors.ConnectionFailure, fetched 2026-11-08T16:03:08.4363995+08:00)pymongo.errors.OperationFailure(exception) — official text: “exception pymongo.errors.OperationFailure(error, code=None, details=None, max_wire_version=None) — Raised when a database operation fails.” (source: https://pymongo.readthedocs.io/en/stable/api/pymongo/errors.html#pymongo.errors.OperationFailure, fetched 2026-11-08T16:03:08.4363995+08:00)pymongo.errors.DuplicateKeyError(exception) — official text: “exception pymongo.errors.DuplicateKeyError(error, code=None, details=None, max_wire_version=None) — Raised when an insert or update fails due to a duplicate key error.” (source: https://pymongo.readthedocs.io/en/stable/api/pymongo/errors.html#pymongo.errors.DuplicateKeyError, fetched 2026-11-08T16:03:08.4363995+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:03:08.4363995+08:00 via WebFetch 官方 API 页面(collection / errors 子页).
MongoClient→ signature from https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient; official text: “class pymongo.mongo_client.MongoClient(host=‘localhost’, port=27017, document_class=dict, tz_aware=False, connect=True, **kwargs)”collection.insert_one→ signature from https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.insert_one; official text: “insert_one(document, bypass_document_validation=None, session=None, comment=None) — Insert a single document.”collection.find_one→ signature from https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.find_one; official text: “find_one(filter=None, *args, **kwargs) — Get a single document from the database.”
FAQ
- Should I create a client per request?
- No. MongoClient is thread-safe and pools connections; create one per process and reuse it across requests.
- How do I avoid SQL-equivalent injection?
- Filters are documents, so pass values as data, never build filter strings; PyMongo binds them safely.
- What is the difference from Motor?
- PyMongo is synchronous. Motor wraps PyMongo for asyncio, giving the same API with await.
Report a problem with this API docOpens a pre-filled issue on GitHub.