Skip to main content

A MongoDB aggregation generator for Mongoengine

Project description

Aggify

Aggify is a Python library to generate MongoDB aggregation pipelines

Package version Downloads Supported Python versions Coverage License Contributors Telegram

Aggify

Aggify is a Python library for generating MongoDB aggregation pipelines, designed to work seamlessly with Mongoengine. This library simplifies the process of constructing complex MongoDB queries and aggregations using an intuitive and organized interface.

Features

  • Programmatically build MongoDB aggregation pipelines.
  • Filter, project, group, and perform various aggregation operations with ease.
  • Supports querying nested documents and relationships defined using Mongoengine.
  • Encapsulates aggregation stages for a more organized and maintainable codebase.
  • Designed to simplify the process of constructing complex MongoDB queries.

TODO

Installation

You can install Aggify using pip:

pip install aggify

Sample Usage

Here's a code snippet that demonstrates how to use Aggify to construct a MongoDB aggregation pipeline:

from mongoengine import Document, fields

class AccountDocument(Document):
    username = fields.StringField()
    display_name = fields.StringField()
    phone = fields.StringField()
    is_verified = fields.BooleanField()
    disabled_at = fields.LongField()
    deleted_at = fields.LongField()
    banned_at = fields.LongField()

class FollowAccountEdge(Document):
    start = fields.ReferenceField("AccountDocument")
    end = fields.ReferenceField("AccountDocument")
    accepted = fields.BooleanField()
    meta = {
        "collection": "edge.follow.account",
    }

class BlockEdge(Document):
    start = fields.ObjectIdField()
    end = fields.ObjectIdField()
    meta = {
        "collection": "edge.block",
    }

Aggify query:

from models import *
from aggify import Aggify, F, Q
from bson import ObjectId

aggify = Aggify(AccountDocument)

pipelines = list(
    (
        aggify.filter(
            phone__in=[],
            id__ne=ObjectId(),
            disabled_at=None,
            banned_at=None,
            deleted_at=None,
            network_id=ObjectId(),
        )
        .lookup(
            FollowAccountEdge,
            let=["id"],
            query=[Q(start__exact=ObjectId()) & Q(end__exact="id")],
            as_name="followed",
        )
        .lookup(
            BlockEdge,
            let=["id"],
            as_name="blocked",
            query=[
                (Q(start__exact=ObjectId()) & Q(end__exact="id"))
                | (Q(end__exact=ObjectId()) & Q(start__exact="id"))
            ],
        )
        .filter(followed=[], blocked=[])
        .group("username")
        .annotate(annotate_name="phone", accumulator="first", f=F("phone") + 10)
        .redact(
            value1="phone",
            condition="==",
            value2="132",
            then_value="keep",
            else_value="prune",
        )
        .project(username=0)[5:10]
        .out(coll="account")
    )
)

Mongoengine equivalent query:

[
    {
        "$match": {
            "phone": {"$in": []},
            "_id": {"$ne": ObjectId("65486eae04cce43c5469e0f1")},
            "disabled_at": None,
            "banned_at": None,
            "deleted_at": None,
            "network_id": ObjectId("65486eae04cce43c5469e0f2"),
        }
    },
    {
        "$lookup": {
            "from": "edge.follow.account",
            "let": {"id": "$_id"},
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$and": [
                                {
                                    "$eq": [
                                        "$start",
                                        ObjectId("65486eae04cce43c5469e0f3"),
                                    ]
                                },
                                {"$eq": ["$end", "$$id"]},
                            ]
                        }
                    }
                }
            ],
            "as": "followed",
        }
    },
    {
        "$lookup": {
            "from": "edge.block",
            "let": {"id": "$_id"},
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$or": [
                                {
                                    "$and": [
                                        {
                                            "$eq": [
                                                "$start",
                                                ObjectId("65486eae04cce43c5469e0f4"),
                                            ]
                                        },
                                        {"$eq": ["$end", "$$id"]},
                                    ]
                                },
                                {
                                    "$and": [
                                        {
                                            "$eq": [
                                                "$end",
                                                ObjectId("65486eae04cce43c5469e0f5"),
                                            ]
                                        },
                                        {"$eq": ["$start", "$$id"]},
                                    ]
                                },
                            ]
                        }
                    }
                }
            ],
            "as": "blocked",
        }
    },
    {"$match": {"followed": [], "blocked": []}},
    {"$group": {"_id": "$username", "phone": {"$first": {"$add": ["$phone", 10]}}}},
    {
        "$redact": {
            "$cond": {
                "if": {"$eq": ["phone", "132"]},
                "then": "$$KEEP",
                "else": "$$PRUNE",
            }
        }
    },
    {"$project": {"username": 0}},
    {"$skip": 5},
    {"$limit": 5},
    {"$out": "account"},
]

In the sample usage above, you can see how Aggify simplifies the construction of MongoDB aggregation pipelines by allowing you to chain filters, lookups, and other operations to build complex queries. For more details and examples, please refer to the documentation and codebase.

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

aggify-0.3.4.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

aggify-0.3.4-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file aggify-0.3.4.tar.gz.

File metadata

  • Download URL: aggify-0.3.4.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for aggify-0.3.4.tar.gz
Algorithm Hash digest
SHA256 d46693cdc271c4f5a0662eb77ec674bb661f9af0718bc6eb6b796d7f34bfd9f3
MD5 2b67f240a951d62fd6a02deaec43b728
BLAKE2b-256 734a903c49e8202dd79f6e87cd7dce85d3ebc525731b42eff970a4e752c084a7

See more details on using hashes here.

Provenance

File details

Details for the file aggify-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: aggify-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for aggify-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 79393db1d5484e1d3226cf282355d6197c7fcfaad0fc41a5fbf203cb2f72ba6e
MD5 e156f394d81b431ae75b8e4ab2fc5595
BLAKE2b-256 5492ec1cf5b3b0da82c80ceb7c3f78b5cba3207177bfa652726b274c34f83c52

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page