Skip to main content

A fluent, type-safe query builder for MongoDB with support for aggregations and Atlas Search

Project description

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)

Atlas Search

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

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

mongodb_query_builder-1.0.1.tar.gz (59.3 kB view details)

Uploaded Source

Built Distribution

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

mongodb_query_builder-1.0.1-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file mongodb_query_builder-1.0.1.tar.gz.

File metadata

  • Download URL: mongodb_query_builder-1.0.1.tar.gz
  • Upload date:
  • Size: 59.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for mongodb_query_builder-1.0.1.tar.gz
Algorithm Hash digest
SHA256 034c9d059cd36c9d83d99cfd1b4c7bca49e726d62309ca66145b6bb45c517dc5
MD5 e9699ebc66c2fd5cb5a8c7937f581fa1
BLAKE2b-256 849fc6d5a89daf96d8c44feb30a0cb29d92696c29470fbec6896d335e852b743

See more details on using hashes here.

File details

Details for the file mongodb_query_builder-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mongodb_query_builder-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40cc3d84e35bf1128d670035e864d6879dee7e4e27daa1a008d8959916963f3d
MD5 76910a28ce33f158b0c19ea1e22a08ef
BLAKE2b-256 f01bd93dabea05aed584411bc5b42a0826c5f98dc827fd5b42a9799722924a20

See more details on using hashes here.

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