Skip to main content

📊 Monggregate

📋 Overview

Monggregate is a library that aims at simplifying usage of MongoDB aggregation pipelines in Python. It's a lightweight QueryBuilder for MongoDB aggregation pipelines based on pydantic and compatible with all mongodb drivers and ODMs.

✨ Features

  • 🔄 Provides an Object Oriented Programming (OOP) interface to the aggregation pipeline.
  • 🎯 Allows you to focus on your requirements rather than MongoDB syntax.
  • 📚 Integrates all the MongoDB documentation and allows you to quickly refer to it without having to navigate to the website.
  • 🔍 Enables autocompletion on the various MongoDB features.
  • 🔗 Offers a pandas-style way to chain operations on data.
  • 💻 Mimics the syntax of your favorite tools like pandas

📥 Installation

💡 The package is available on PyPI:

pip install monggregate

🚀 Usage

📘 The below examples reference the MongoDB sample_mflix database

🔰 Basic Pipeline usage

import os

from dotenv import load_dotenv 
import pymongo
from monggregate import Pipeline, S

# Creating connexion string securely
# You need to create a .env file with your password
load_dotenv(verbose=True)
MONGODB_URI = os.environ["MONGODB_URI"] 


# Connect to your MongoDB cluster:
client = pymongo.MongoClient(MONGODB_URI)

# Get a reference to the "sample_mflix" database:
db = client["sample_mflix"]

# Creating the pipeline
pipeline = Pipeline()

# The below pipeline will return the most recent movie with the title "A Star is Born"
pipeline.match(
    title="A Star Is Born"
).sort(
    by="year"
).limit(
    value=1
)

# Executing the pipeline
curosr = db["movies"].aggregate(pipeline.export())

# Printing the results
results = list(curosr)
print(results)

🌟 Advanced Usage, with MongoDB Operators

import os

from dotenv import load_dotenv 
import pymongo
from monggregate import Pipeline, S


# Creating connexion string securely
load_dotenv(verbose=True)
MONGODB_URI = os.environ["MONGODB_URI"]

# Connect to your MongoDB cluster:
client = pymongo.MongoClient(MONGODB_URI)

# Get a reference to the "sample_mflix" database:
db = client["sample_mflix"]


# Creating the pipeline
pipeline = Pipeline()
pipeline.match(
    year=S.type_("number") # Filtering out documents where the year field is not a number
).group(
    by="year",
    query = {
        "movie_count":S.sum(1), # Aggregating the movies per year
        "movie_titles":S.push("$title")
    }
).sort(
    by="_id",
    descending=True
).limit(10)

# Executing the pipeline
cursor = db["movies"].aggregate(pipeline.export())

# Printing the results
results = list(cursor)
print(results)

🔥 Even More Advanced Usage with Expressions

import os

from dotenv import load_dotenv 
import pymongo
from monggregate import Pipeline, S

# Creating connexion string securely
load_dotenv(verbose=True)
MONGODB_URI = os.environ["MONGODB_URI"] 

# Connect to your MongoDB cluster:
client = pymongo.MongoClient(MONGODB_URI)

# Get a reference to the "sample_mflix" database:
db = client["sample_mflix"]

# Using expressions
comments_count = S.size(S.comments)

# Creating the pipeline
pipeline = Pipeline()
pipeline.lookup(
    right="comments",
    right_on="movie_id",
    left_on="_id",
    name="comments"
).add_fields(
    comments_count=comments_count
).match(
    expression=comments_count>2
).limit(1)

# Executing the pipeline
cursor = db["movies"].aggregate(pipeline.export())

# Printing the results
results = list(cursor)
print(results)

🔍 Going Further

Metadata

Release files for monggregate 0.22.1

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

Source distribution (sdist)

Source distribution for monggregate 0.22.1
File Size Uploaded
monggregate-0.22.1.tar.gz 97.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for monggregate 0.22.1
File Interpreter ABI Platform
monggregate-0.22.1-py3-none-any.whl Python 3 none any Details

Total release size: 267.0 kB

Release files / monggregate-0.22.1.tar.gz

Download URL monggregate-0.22.1.tar.gz
Size 97.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6c36b5cea8ec51bd4a36e25f149d6f4b424060b9a8e023ef909884ae500b8c3a
BLAKE2b-256 checksum
How to use checksums
e28c9627d0d3a569a2e1c6657dcd27ff7593c33f07efc5c6d206c7bf27b58c78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release files / monggregate-0.22.1-py3-none-any.whl

Download URL monggregate-0.22.1-py3-none-any.whl
Size 169.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4eef7839109ce4b1bb1172b6643fa22e2dc284a45e645ea55fd4efd848aedfb2
BLAKE2b-256 checksum
How to use checksums
38772f37358731fdf228379fb9bdc0c736371691c0493075c393ee431e42b908
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release history Release notifications | RSS feed

This release

0.22.1 This release

2 release files

0.22.0

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.16.2

2 release files

0.16.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.1

1 release file

0.10.0

1 release file

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

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