Skip to content

Mongo Aggro

MongoDB Aggregation Pipeline Builder with Pydantic

CI PyPI Python Versions License


Mongo Aggro is a Python library that provides a type-safe, Pydantic-based interface for building MongoDB aggregation pipelines. It offers strong type checking, IDE autocompletion, and a clean, Pythonic API.

Features

  • 🔒 Type Safety - Full Pydantic v2 support with proper type hints
  • 🔗 Seamless Integration - Works directly with PyMongo's aggregate() method
  • 📦 25+ Stages - All major MongoDB aggregation stages supported
  • 📊 18 Accumulators - Sum, Avg, Min, Max, Push, and more
  • 🔍 Query Operators - And, Or, In, Regex, and other operators
  • 🎯 IDE Support - Full autocompletion and type checking in your IDE

Quick Example

from mongo_aggro import Pipeline, Match, Group, Sort, Limit, Sum, Avg

# Build a pipeline
pipeline = Pipeline([
    Match(query={"status": "active"}),
    Group(
        id="$category",
        accumulators={
            "total": {"$sum": "$amount"},
            "count": {"$sum": 1},
        }
    ),
    Sort(fields={"total": -1}),
    Limit(count=10),
])

# Use directly with PyMongo
results = collection.aggregate(pipeline)

Installation

pip install mongo-aggro

Or with Poetry:

poetry add mongo-aggro

Documentation

License

MIT License - see LICENSE for details.