Skip to main content

MongoDB Query Builder

A fluent, type-safe query builder for MongoDB with support for complex queries, aggregation pipelines, and Atlas Search.

Features

✨ Fluent API - Chain methods for readable query construction
🔒 Type-safe - Full type hints and validation
📦 Comprehensive - Support for queries, aggregations, and Atlas Search
🎯 Intuitive - Pythonic interface for MongoDB operations
✅ Well-tested - Extensive test coverage
📚 Well-documented - Complete API documentation and examples

Installation

pip install mongodb-query-builder

Quick Start

Simple Query

from mongodb_query_builder import QueryFilter

# Build a query filter
query = QueryFilter()\
    .field("age").greater_than(18)\
    .field("status").equals("active")\
    .build()

# Use with pymongo
collection.find(query)

Aggregation Pipeline

from mongodb_query_builder import AggregateBuilder, QueryFilter

# Build aggregation pipeline
pipeline = AggregateBuilder()\
    .match(QueryFilter().field("status").equals("active"))\
    .group(by="$category", count={"$sum": 1}, total={"$sum": "$amount"})\
    .sort("count", ascending=False)\
    .limit(10)\
    .build()

# Use with pymongo
results = collection.aggregate(pipeline)
from mongodb_query_builder import AtlasSearchBuilder, CompoundBuilder

# Simple text search
search = AtlasSearchBuilder()\
    .text("python developer", path=["title", "description"])\
    .build_stage()

# Complex compound search
compound = CompoundBuilder()
compound.must().text("python", path="skills")
compound.should().text("senior", path="level", score=2.0)
compound.filter().range("experience", gte=3)

search = AtlasSearchBuilder()\
    .compound(compound)\
    .facet("departments", type="string", path="department")\
    .build_stage()

# Use in aggregation
pipeline = [search, {"$limit": 10}]
results = collection.aggregate(pipeline)

Core Components

QueryFilter

Build MongoDB query filters with a fluent interface:

from mongodb_query_builder import QueryFilter

# Comparison operators
q = QueryFilter().field("age").greater_than(18)
q = QueryFilter().field("price").between(10, 100)

# String operations
q = QueryFilter().field("name").starts_with("John")
q = QueryFilter().field("email").contains("@example.com")

# Array operations
q = QueryFilter().field("tags").array_contains("python")
q = QueryFilter().field("skills").array_contains_all(["python", "mongodb"])

# Logical operators
q = QueryFilter().any_of([
    QueryFilter().field("role").equals("admin"),
    QueryFilter().field("role").equals("moderator")
])

AggregateBuilder

Build aggregation pipelines:

from mongodb_query_builder import AggregateBuilder

pipeline = AggregateBuilder()\
    .match(QueryFilter().field("status").equals("active"))\
    .group(by="$category", count={"$sum": 1})\
    .sort("count", ascending=False)\
    .limit(10)\
    .project(category="$_id", count=1, _id=0)\
    .build()

AtlasSearchBuilder

Build Atlas Search queries:

from mongodb_query_builder import AtlasSearchBuilder, CompoundBuilder

# Text search with fuzzy matching
search = AtlasSearchBuilder()\
    .text("search term", path="title", fuzzy={"maxEdits": 2})\
    .build_stage()

# Compound search
compound = CompoundBuilder()
compound.must().text("required term", path="content")
compound.should().text("optional term", path="tags", score=2.0)
compound.filter().range("date", gte=datetime(2024, 1, 1))

search = AtlasSearchBuilder().compound(compound).build_stage()

Requirements

  • Python 3.8+
  • pymongo (optional, for MongoDB operations)
  • typing-extensions (for Python < 3.10)

Documentation

📚 Getting Started

🔧 API Reference

📖 Guides

🛠️ Development

📐 Full Documentation

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Support

Metadata

Release files for mongodb-query-builder 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongodb-query-builder 1.1.0
File Size Uploaded Provenance Actions
mongodb_query_builder-1.1.0.tar.gz 92.0 kB Without provenance Details for mongodb_query_builder-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mongodb-query-builder 1.1.0
File Interpreter ABI Platform Actions
mongodb_query_builder-1.1.0-py3-none-any.whl Python 3 none any Details for mongodb_query_builder-1.1.0-py3-none-any.whl

Total release size: 118.7 kB

Release files / mongodb_query_builder-1.1.0.tar.gz

Download URL mongodb_query_builder-1.1.0.tar.gz
Size 92.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7c361f0f6d0eb64c11229396cfb98682abce91a8a00c25f167220c949568b0d9
BLAKE2b-256 checksum
How to use checksums
866eb902b0ad9b6ebc7d62803451f2e73bf5790ad713c1ee48f4b25562d90d31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / mongodb_query_builder-1.1.0-py3-none-any.whl

Download URL mongodb_query_builder-1.1.0-py3-none-any.whl
Size 26.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5d986a4e11420aaf78e5ef714e479d3128a381cbe7c02c41b03f37b9cce833a
BLAKE2b-256 checksum
How to use checksums
9c4d82d293bc8c782b7da7305a764f3105e653139729a7a7cc88d88a110ccfb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Security for mongodb-query-builder 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page