
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
mongoex
Advanced tools
Welcome to MongoEX! This project is a modern Python library for building MongoDB queries with full type safety, IDE autocompletion, and compile-time validation. My goal is to make your database interactions as safe and developer-friendly as possible.
from dataclasses import dataclass
from mongoex import PipelineBuilder
from mongoex.operators import Sum
@dataclass
class Sale:
item: str
price: float
quantity: int
# Build a pipeline with type-safe field access
builder = PipelineBuilder(model=Sale)
pipeline = (
builder.match(builder.fields.price > 50)
.group(by="item", total_revenue=Sum("price"))
)
# The builder can be used directly with PyMongo
# result = db.sales.aggregate(pipeline)
# The generated pipeline:
# [
# {'$match': {'price': {'$gt': 50}}},
# {'$group': {'_id': '$item', 'total_revenue': {'$sum': '$price'}}}
# ]
examples/01_basic_pipeline.py: A basic example of building a pipeline.examples/02_advanced_grouping.py: Demonstrating advanced grouping operations and output validation.examples/03_json_to_mongoex.py: How to convert a JSON pipeline to MongoEX code.examples/04_logical_operations.py: Demonstrating AND and OR operations.examples/05_error_handling.py: Demonstrating the enhanced error reporting.examples/06_complex_analytics_pipeline.py: A complex, multi-stage analytics pipeline example.MongoEX introduces a powerful Model-Based Aggregation system that allows you to define aggregation pipelines using strongly-typed dataclass models. This approach provides enhanced type safety, better maintainability, and clear versioning of data transformations.
Annotated[type, Operator] syntaxfrom typing import Annotated
from dataclasses import dataclass
from mongoex import PipelineBuilder
from mongoex.operators import Sum, Avg, First
@dataclass
class CategoryAnalysis:
"""Revenue analysis model for product categories."""
_id: str # The grouped field (category name)
total_revenue: Annotated[float, Sum("price")]
avg_price: Annotated[float, Avg("price")]
transaction_count: Annotated[int, Sum(1)]
first_sale: Annotated[str, First("item")]
# Usage with model-based grouping
builder = PipelineBuilder(model=Sale)
pipeline = (
builder.match(builder.fields.price > 10)
.group(by="category", model=CategoryAnalysis) # ← Type-safe model
.sort_by(total_revenue=-1)
)
# Generates identical pipeline to traditional approach, but with full type safety
Model Versioning for Data Transformations:
@dataclass
class CustomerAnalysisV1:
"""Version 1: Basic customer metrics."""
_id: str
total_spent: Annotated[float, Sum("amount")]
order_count: Annotated[int, Sum(1)]
@dataclass
class CustomerAnalysisV2:
"""Version 2: Enhanced with lifetime value calculation."""
_id: str
total_spent: Annotated[float, Sum("amount")]
order_count: Annotated[int, Sum(1)]
avg_order_value: Annotated[float, Avg("amount")]
first_order_date: Annotated[str, First("order_date")]
Pipeline Stage Composition:
# Build complex analytics with multiple model-based stages
analytics_pipeline = (
builder.match(builder.fields.status == "completed")
.group(by="customer_id", model=CustomerLifetimeValue)
.match(builder.fields.total_spent > 1000) # High-value customers
.sort_by(lifetime_value=-1)
)
Sum("field") - Sum values from a fieldSum(1) - Count documents (literal count)Avg("field") - Average values from a fieldFirst("field") - First value in groupLast("field") - Last value in groupYou can find some syntax examples / tutorials at tutorials folder
MongoEX is currently in an early development stage. I am actively working on expanding the API, improving performance, and adding support for more MongoDB operators.
I welcome contributions! To get started, please check out the development workflow in our makefile.
make format lint: Format and lint your code.make type-check: Run the type checker.make test: Run the full test suite.FAQs
MongoDB query building using Python's AST module with type-safe fluent APIs
We found that mongoex demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.