Skip to main content

Python MongoDB aggregation ORM

Project description

mongo_aggregation

Python MongoDB Aggregation ORM class.

Installation

pip install mongo_aggregation

Usage examples

import pymongo
from datetime import datetime
from dateutil.relativedelta import relativedelta
from mongo_aggregation import MongoAggregation

# Usual pymongo connection
client = pymongo.MongoClient('mongodb://localhost:27017/test')
db = client.get_database()
today = datetime.now()
yesterday = today - relativedelta(days=1)

# Compose the pipeline
pipeline = MongoAggregation(collection=db.action)
pipeline.match(
    {'date': {'$gte': yesterday, '$lt': today}},
    completed=True,
    _cls={'$in': [
        'Action.Order', 'Action.StorageIncome', 'Action.StorageCancellation', 'Action.StorageMovementOutcome'
    ]},
).smart_project(
    'transactions.amount,_cls', '_id'
).append([
    {'$project': {
        '_cls': 1,
        'date': 1,
        'transactions.amount': 1,
        'transactions.cashbox': 1,
    }},
]).project(
    {'transactions.amount': 1, 'transactions.cashbox': 1}, _cls=1, date=1
).project(
    transactions=1, _cls=1, date=1
)
# Run it
cursor = pipeline.aggregate()

# Iterate over result
for doc in cursor:
    print(doc)
    break

Methods description

pipeline.replace_root('doctor')
# {'$replaceRoot': {'newRoot': '$doctor'}}

Response as list

By default aggregate returns cursor. If you want it to return a list of documents use as_list argument:

data = pipeline.aggregate(as_list=True)

Patterns module

Provides operators and some other patterns in python functions way.

Imports:

from mongo_aggregation.patterns import regex
and_({'a': True}, b=True, c=True)
# {'$and': [{'a': True}, {'b': True}, {'c': True}]}
or_({'a': True}, b=True, c=True)
# {'$or': [{'a': True}, {'b': True}, {'c': True}]}
regex('name', i=True)
# {'$regex': 'name', '$options': 'i'}

Aggregation patterns module

Provides aggregation operators and some other patterns in python functions way.

Imports:

from mongo_aggregation.aggr_patterns import merge_objects
merge_objects('$doctor', first_name='John', last_name='Doe')
# {'$mergeObjects': ['$doctor', {'first_name': 'John', 'last_name': 'Doe'}]}

Changelog

1.0.4 (2020-09-25)

  • Bug fixes.

1.0.3 (2020-09-01)

  • .sort now supports -email sorting format (direction and field name by string).
  • Added .replace_root method.
  • Added or_ and and_ logical operators. _and is deprecated now.
  • Added regex operator pattern.
  • Added merge_objects aggregation operator (./aggr_patterns).

1.0.2 (2020-09-01)

  • Added $addFields and $set stages.
  • Added $filter operator.

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

mongo-aggregation-1.0.4.tar.gz (13.7 kB view details)

Uploaded Source

File details

Details for the file mongo-aggregation-1.0.4.tar.gz.

File metadata

  • Download URL: mongo-aggregation-1.0.4.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

File hashes

Hashes for mongo-aggregation-1.0.4.tar.gz
Algorithm Hash digest
SHA256 20c2e55ca4ad24297943d762d93172493c5695a20db5e54e9e105c6b6da7eb5c
MD5 8394dc4b8d35362c2926e33a91daefe8
BLAKE2b-256 60456b35b508c7a1b1ccb45a3abda98653f60e7d2e7b3ed1b0061762ddd10f01

See more details on using hashes here.

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