MongoDB aggregation pipelines made easy. Joins, grouping, counting and much more...
Reason this release was yanked:
Broken
Project description
Overview
Monggregate is a library that aims at simplifying usage of MongoDB aggregation pipelines in Python. It is based on MongoDB official Python driver, pymongo and on pydantic.
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.
Requirements
This package requires python > 3.10, pydantic > 1.8.0
Installation
The repo is now 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)
PWD = os.environ["MONGODB_PASSWORD"]
MONGODB_URI = f"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority"
# 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)
PWD = os.environ["MONGODB_PASSWORD"]
MONGODB_URI = f"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority"
# 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, Expression
# Creating connexion string securely
load_dotenv(verbose=True)
PWD = os.environ["MONGODB_PASSWORD"]
MONGODB_URI = f"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority"
# 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 = Expression.field("comments").size()
# 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
- Check out the full documentation for more examples.
- Check out this medium article.
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
monggregate-0.19.0.tar.gz
(91.8 kB
view details)
Built Distribution
monggregate-0.19.0-py3-none-any.whl
(138.1 kB
view details)
File details
Details for the file monggregate-0.19.0.tar.gz
.
File metadata
- Download URL: monggregate-0.19.0.tar.gz
- Upload date:
- Size: 91.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59e0ec241076550099cd57163248be78127ea5a4fee86881ceeb152082f24e99 |
|
MD5 | 0980bacd4c2f0a623b4d02ba8df10e6a |
|
BLAKE2b-256 | ff5f14de478999c016a1dd49a83660cd398e798f3797321b17dff444417f7fa0 |
File details
Details for the file monggregate-0.19.0-py3-none-any.whl
.
File metadata
- Download URL: monggregate-0.19.0-py3-none-any.whl
- Upload date:
- Size: 138.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 883c0b30401d9852e584a3eda37d64ea2d11ea40bba26ff00c1cfa519e31ffca |
|
MD5 | 501b5b30e47622bd58b523c0a4668baa |
|
BLAKE2b-256 | 22e1fe1bfa038b1b263a7049f2c20c1ee018e7d61bcf0dcf6f2ff5dd0812dc0c |