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 license contributors

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

  • $replaceRoot: Replaces the document structure with a new one.
  • $project (with expressions): Allows you to use expressions to reshape and calculate values.
  • $redact: Controls document inclusion during the aggregation pipeline.

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 PostDocument(Document):
    owner = fields.ReferenceField('AccountDocument', db_field='owner_id')
    caption = fields.StringField()
    location = fields.StringField()
    comment_disabled = fields.BooleanField()
    stat_disabled = fields.BooleanField()
    hashtags = fields.ListField()
    archived_at = fields.LongField()
    deleted_at = fields.LongField()

Aggify query:

from aggify import Aggify, Q, F

query = Aggify(PostDocument)

query.filter(deleted_at=None, caption__contains='Aggify').order_by('-_id').lookup(
        AccountDocument, query=[
            Q(_id__exact='owner') & Q(deleted_at=None),
            Q(is_verified__exact=True)
        ], let=['owner'], as_name='owner'
    ).filter(owner__ne=[]).add_fields({
        "aggify": "Aggify is lovely",
    }
    ).project(caption=0).out("post").pipelines

Mongoengine equivalent query:

[
        {
            '$match': {
                'caption': {
                    '$options': 'i',
                    '$regex': '.*Aggify.*'
                },
                'deleted_at': None
            }
        },
        {
            '$sort': {
                '_id': -1
            }
        },
        {
            '$lookup': {
                'as': 'owner',
                'from': 'account',
                'let': {
                    'owner': '$owner_id'
                },
                'pipeline': [
                    {
                        '$match': {
                            '$expr': {
                                '$and': [
                                    {
                                        '$eq': ['$_id', '$$owner']
                                    },
                                    {
                                        'deleted_at': None
                                    }
                                ]
                            }
                        }
                    },
                    {
                        '$match': {
                            '$expr': {
                                '$eq': ['$is_verified', True]
                            }
                        }
                    }
                ]
            }
        },
        {
            '$match': {
                'owner': {'$ne': []}
            }
        },
        {
            '$addFields': {
                'aggify': {
                    '$literal': 'Aggify is lovely'
                }
            }
        },
        {
            '$project': {
                'caption': 0
                }
        },
        {
            '$out': 'post'
        }
]

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.2.4.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

aggify-0.2.4-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aggify-0.2.4.tar.gz
Algorithm Hash digest
SHA256 97b11c17e28e1b9940a57a900d2e82a547d49aa4ae69ff5da598b53c52cf5d86
MD5 34af52c7d9e46c484ecf85082900c84d
BLAKE2b-256 d2dd3f2db6ba8c571c58a3320d2e1f8ea76b9bd0308b183ca6a78af8a7bb6309

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: aggify-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 14.8 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.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3523afd646fa7ad4efda0718b677fe9cafdf51e61767cacf1f5ebed52121a754
MD5 3e18d96f8a20bcb1a81c81efbabee811
BLAKE2b-256 7e344432cdbfc7b77cbeb200ac718baed088addc14ead7d21a38ade855f838ca

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