Skip to main content

Python MongoDB aggregation ORM

Project description

mongo_aggregation

Python MongoDB Aggregation ORM class.

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

Response as list

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

cursor = pipeline.aggregate(as_list=True)

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

Uploaded Source

Built Distribution

mongo_aggregation-1.0.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mongo-aggregation-1.0.0.tar.gz
  • Upload date:
  • Size: 10.6 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.0.tar.gz
Algorithm Hash digest
SHA256 7e53ff2666fe3dd4a9f13cffa109d7201aaf9730b91800322e5ee400d25b0bf6
MD5 59b97bd0d215311d06411a8f26b72a44
BLAKE2b-256 cca0e02dc900b94c074da95def32fb27681149549ddf45e7553203b07038cd17

See more details on using hashes here.

File details

Details for the file mongo_aggregation-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: mongo_aggregation-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21c7f860ea8935e3d7a658076bd5dcc61aa86311120a5f03426f3cc4bd97520b
MD5 b3279dc072727a65584ad081a4179740
BLAKE2b-256 74e793d61658075fbeef3cd12323511194931a3bbc56b866ef93d346aa22779a

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