MongoDB Aggregation Pipeline Builder with Pydantic
Project description
Mongo Aggro
A Python package for building MongoDB aggregation pipelines with Pydantic.
Features
- Type-safe - Pydantic models with full type hints
- Zero-conversion - Pass
Pipelinedirectly tocollection.aggregate() - Python operators - Use
F("age") > 18instead 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5c7ed63726a52b6d00e5be0ee68dc7bc2fb2f031062d9b669ee169e0dec822a
|
|
| MD5 |
9a7acfcce82b5df6f5f3916e0be9298c
|
|
| BLAKE2b-256 |
6a0ced0c9648618cece06cf4e05af7dba904ebfa2c3b8438f969aef96af0665d
|
Provenance
The following attestation bundles were made for mongo_aggro-0.2.2.tar.gz:
Publisher:
ci.yml on hamedghenaat/mongo-aggro
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mongo_aggro-0.2.2.tar.gz -
Subject digest:
f5c7ed63726a52b6d00e5be0ee68dc7bc2fb2f031062d9b669ee169e0dec822a - Sigstore transparency entry: 914666574
- Sigstore integration time:
-
Permalink:
hamedghenaat/mongo-aggro@63c3bbc85dde469d0411d5936bd7ac551f67c307 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hamedghenaat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@63c3bbc85dde469d0411d5936bd7ac551f67c307 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55eac4d3717c7721dc57aa580164ed8a15c383124d4a1de15a3c928f88a74afc
|
|
| MD5 |
a182b7de5f6298e7d35eb1d1d997c7a5
|
|
| BLAKE2b-256 |
6ccb05421fff8090bea1685e107101635335a528aa105416836e9ddd0f6e0a4a
|
Provenance
The following attestation bundles were made for mongo_aggro-0.2.2-py3-none-any.whl:
Publisher:
ci.yml on hamedghenaat/mongo-aggro
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mongo_aggro-0.2.2-py3-none-any.whl -
Subject digest:
55eac4d3717c7721dc57aa580164ed8a15c383124d4a1de15a3c928f88a74afc - Sigstore transparency entry: 914666670
- Sigstore integration time:
-
Permalink:
hamedghenaat/mongo-aggro@63c3bbc85dde469d0411d5936bd7ac551f67c307 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hamedghenaat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@63c3bbc85dde469d0411d5936bd7ac551f67c307 -
Trigger Event:
push
-
Statement type: