FastAPI integration for PyMongo:
PyObjectId: Pydantic-nativeObjectId. Validates fromObjectIdor hex string, serializes to a string in JSON mode (Python-mode dumps keep theObjectId, so documents round-trip throughinsert_one), correct OpenAPI/JSON schema.MongoSettings:pydantic-settingsbase class for Mongo URI and database name. Subclass to add app settings.mongo_lifespan/get_client/get_database: client lifecycle wired to FastAPI'slifespan, exposed viaDepends().beanie_lifespan(optional,fastapi-pymongo[beanie]): runs Beanie'sinit_beaniealongside the rest offastapi_pymongo, for an ODM on top.
pip install fastapi-pymongoOptional Beanie integration:
pip install "fastapi-pymongo[beanie]"Optional cursor pagination via fastapi-pagination:
pip install "fastapi-pymongo[pagination]"examples/quickstart: minimal CRUD app, raw PyMongo.examples/beanie: same CRUD app on Beanie. Verified against a real MongoDB instance (create/list/get/delete).
pip install -e ".[dev]"
pytest # unit and integration tests (perf is deselected by default)
pytest -m perf # PyObjectId overhead benchmarks
ruff check src tests examples
mypy srcIntegration tests need a running MongoDB; set MONGODB_URI (default
mongodb://localhost:27017). Run the full suite with a local server, for
example via mongodb-runner start --topology standalone.
Sphinx docs (furo theme) under docs/source:
pip install -e ".[docs]"
sphinx-build -b html docs/source docs/build
open docs/build/index.html