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

pippip install pymongo

Methods

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:

API reference evidence

Fetched from official sources on 2026-11-08T16:03:08.4363995+08:00 via WebFetch 官方 API 页面(collection / errors 子页).

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.