skillfed

pydantic-mongo

Document object mapper for pydantic and pymongo

pydantic-mongo v3.1.0 182.3K downloads/30d#10,104 on PyPI169
Permissive license MIT AGING released

What it is and what it does

Pydantic Mongo is a thin wrapper around pymongo that applies the Repository pattern, letting you define MongoDB collections as Pydantic models and interact with them through a type-safe, validated interface. It handles the boilerplate of connecting Pydantic's validation and serialization to MongoDB's document model, so you define your schema once as a Pydantic class and then use repository methods (save, find_one_by_id, find_by, delete, etc.) to perform CRUD operations without writing raw MongoDB queries.

The package supports both synchronous (via AbstractRepository) and asynchronous (via AsyncAbstractRepository) workflows, making it suitable for blocking and non-blocking applications. It depends on pymongo for the driver layer and pydantic for validation, so you inherit both their capabilities and constraints—notably, you still need a MongoDB server running, and your models must be valid Pydantic classes.

Use it for:

  • Building a REST API where each endpoint maps to a Pydantic model stored in MongoDB, with automatic validation on save.
  • Writing async FastAPI handlers that query MongoDB without writing raw pymongo code or managing connection pooling manually.
  • Migrating from a different ORM to MongoDB while keeping your existing Pydantic schemas and validation logic.
  • Implementing cursor-based pagination for large result sets in a web application.
  • Defining type-safe repository classes for unit testing with mock repositories that share the same interface.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides a Repository pattern for MongoDB with Pydantic model integration, supporting both synchronous and asynchronous CRUD operations with built-in validation and serialization.

Yes, if you are already using Pydantic and MongoDB together. The package reduces boilerplate and provides a clean, type-safe interface for common CRUD patterns. Maintenance is aging but active, with no security issues. Install friction is low. The main trade-off is that you are adding a thin abstraction layer—if you need fine-grained control over MongoDB queries or aggregation pipelines, you may find the repository pattern limiting and prefer working directly with pymongo.

Install

pydantic-mongo on PyPI

pip

pip install pydantic-mongo

uv

uv add pydantic-mongo

poetry

poetry add pydantic-mongo

Installing pydantic-mongo

Before you install

Low install friction with a pure Python wheel. Maintenance is aging—last commit was 2025-10-13, but the project remains active and supports current Python versions (3.9, 3.10, 3.11). No known vulnerabilities.

License in practice

MIT license is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install pydantic-mongo

from pydantic import BaseModel
from pydantic_mongo import AbstractRepository
from pymongo import MongoClient

class MyModel(BaseModel):
    name: str

class MyRepository(AbstractRepository[MyModel]):
    class Meta:
        collection_name = 'items'

client = MongoClient('mongodb://localhost:27017')
db = client['mydb']
repo = MyRepository(db)

item = MyModel(name='test')
repo.save(item)

Requires a running MongoDB instance accessible at the connection string you provide.

Verify before relying

  • Whether cursor-based pagination handles large result sets efficiently in practice.
  • Performance characteristics when working with deeply nested Pydantic models.
  • Compatibility guarantees with future versions of pymongo and pydantic.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 2 — pymongo, pydantic
Maintenance aging — 483 days since the last release
Last repo commit
First released
Downloads 182,303/month — #10,104 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pydantic_mongo-3.1.0-py2.py3-none-any.whl

License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.9

Tags

mongodb repository patternpydantic mongo ormasync mongodb operationsdocument mapper pydanticmongodb crud helpertype-safe mongodb queriesmongo repository abstraction
mongodb-ormpydantic-integrationasync-support

More Database packages