Skip to main content

MongoDB Aggregation Pipeline Builder with Pydantic

Project description

Mongo Aggro

A Python package for building MongoDB aggregation pipelines with Pydantic.

PyPI Python License

Features

  • Type-safe - Pydantic models with full type hints
  • Zero-conversion - Pass Pipeline directly to collection.aggregate()
  • Python operators - Use F("age") > 18 instead of {"$gt": ["$age", 18]}
  • 149 expression operators - Arithmetic, string, date, array, and more
  • 46 aggregation stages - All major MongoDB stages supported
  • 31 query predicates - Comparison, logical, geospatial, bitwise
  • 17 accumulators - Sum, Avg, Min, Max, Push, TopN, etc.

Installation

uv add mongo-aggro
# or
pip install mongo-aggro

Quick Start

from mongo_aggro import Pipeline, Match, Group, Sort, Expr
from mongo_aggro.expressions import F

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

# Python operator syntax
pipeline = Pipeline([
    Match(query=Expr(expression=(F("status") == "active")).model_dump()),
    Group(id="$category", accumulators={"total": {"$sum": "$amount"}}),
])

# Pass directly to MongoDB
results = collection.aggregate(pipeline)

Package Structure

# Import from root (most common)
from mongo_aggro import Pipeline, Match, Group, Sort, Expr

# Import expressions from sub-package
from mongo_aggro.expressions import F, AddExpr, ConcatExpr, CondExpr

# Import stages by category
from mongo_aggro.stages import Match, Project, Lookup, SetWindowFields

# Import query operators
from mongo_aggro.operators import In, Regex, GeoWithin, Exists

Expression Operators with Python Syntax

from mongo_aggro import Expr, Match
from mongo_aggro.expressions import F

# Comparison operators
F("age") > 18        # {"$gt": ["$age", 18]}
F("status") == "active"  # {"$eq": ["$status", "active"]}

# Logical operators (use & | ~ instead of and or not)
(F("age") >= 18) & (F("status") == "active")
(F("type") == "premium") | (F("balance") > 1000)

# Use in Match stage
Match(query=Expr(expression=(
    (F("status") == "active") & (F("age") > 18)
)).model_dump())

Stages

Category Stages
Core Match, Project, Group, Sort, Limit, Skip, Unwind, AddFields, Set, Unset
Join Lookup, GraphLookup, UnionWith
Aggregation Bucket, BucketAuto, Facet, SortByCount, Count
Output Out, Merge
Window SetWindowFields, Densify, Fill
Geospatial GeoNear
Search Search, SearchMeta, VectorSearch, RankFusion
Change Stream ChangeStream, ChangeStreamSplitLargeEvent
Admin CollStats, IndexStats, CurrentOp, ListSessions

Expression Operators

Category Examples
Comparison Eq, Ne, Gt, Gte, Lt, Lte, Cmp
Logical And, Or, Not
Arithmetic Add, Subtract, Multiply, Divide, Mod, Abs, Ceil, Floor
String Concat, Split, ToLower, ToUpper, Trim, RegexMatch
Array ArraySize, Filter, Map, Reduce, Slice, InArray
Date DateAdd, DateDiff, DateToString, Year, Month, Day
Type ToDate, ToString, ToInt, Convert, Type
Conditional Cond, IfNull, Switch
Set SetUnion, SetIntersection, SetDifference
Window Rank, DenseRank, Shift, ExpMovingAvg

Query Predicates

from mongo_aggro import Match, In, Regex, Exists, GeoWithin

Match(query={
    "status": In(values=["active", "pending"]).model_dump(),
    "email": Regex(pattern="@company\\.com$").model_dump(),
    "profile": Exists(exists=True).model_dump(),
})

Accumulators

from mongo_aggro import Group, Sum, Avg, Max, Push, merge_accumulators

Group(
    id="$category",
    accumulators=merge_accumulators(
        Sum(name="total", field="amount"),
        Avg(name="average", field="price"),
        Max(name="highest", field="price"),
    )
)

Documentation

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongo_aggro-0.2.2.tar.gz (224.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongo_aggro-0.2.2-py3-none-any.whl (58.9 kB view details)

Uploaded Python 3

File details

Details for the file mongo_aggro-0.2.2.tar.gz.

File metadata

  • Download URL: mongo_aggro-0.2.2.tar.gz
  • Upload date:
  • Size: 224.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mongo_aggro-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f5c7ed63726a52b6d00e5be0ee68dc7bc2fb2f031062d9b669ee169e0dec822a
MD5 9a7acfcce82b5df6f5f3916e0be9298c
BLAKE2b-256 6a0ced0c9648618cece06cf4e05af7dba904ebfa2c3b8438f969aef96af0665d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mongo_aggro-0.2.2.tar.gz:

Publisher: ci.yml on hamedghenaat/mongo-aggro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mongo_aggro-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: mongo_aggro-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 58.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mongo_aggro-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 55eac4d3717c7721dc57aa580164ed8a15c383124d4a1de15a3c928f88a74afc
MD5 a182b7de5f6298e7d35eb1d1d997c7a5
BLAKE2b-256 6ccb05421fff8090bea1685e107101635335a528aa105416836e9ddd0f6e0a4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mongo_aggro-0.2.2-py3-none-any.whl:

Publisher: ci.yml on hamedghenaat/mongo-aggro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page