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
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
mongo-aggregation-1.0.1.tar.gz
(10.8 kB
view details)
Built Distribution
File details
Details for the file mongo-aggregation-1.0.1.tar.gz
.
File metadata
- Download URL: mongo-aggregation-1.0.1.tar.gz
- Upload date:
- Size: 10.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dbe12288294d25d464e265d514d1c2db82c60835e9eba923a0ff84029752c28 |
|
MD5 | 8b65a4d8272e3e3102f8d7a977463a55 |
|
BLAKE2b-256 | 2f6dd05d73d0855f28a5227b6a76df341e3f643efaf7a41c4ea18e7806e8c76b |
File details
Details for the file mongo_aggregation-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: mongo_aggregation-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.6 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | de5a1e254a5e75b8cef3c0951de104b82f37cccacc6a7d195c6b1476b0a05c97 |
|
MD5 | 6a7937ded829b7233007d5253e2972d6 |
|
BLAKE2b-256 | af2bd4a5576679df8131858adf7ed542db6d7b8c4e051cb123616c4f1e766da7 |