Python MongoDB Aggregation ORM class.
pip install mongo_aggregationimport 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)
breakpipeline.sort('-doctor')
# {'$sort': {'doctor': -1}}
pipeline.sort({'doctor': 1})
# {'$sort': {'doctor': 1}}
pipeline.sort('doctor', '-patient')
# [{'$sort': {'doctor': 1}}, {'$sort': {'-patient': 1}}]order_byis an alias tosort.
pipeline.order_by('-doctor')
# {'$sort': {'doctor': -1}}pipeline.replace_root('doctor')
# {'$replaceRoot': {'newRoot': '$doctor'}}Returns count. Does not add stage.
pipeline.count()
2By default aggregate returns cursor. If you want it to return a list of documents use as_list argument:
data = pipeline.aggregate(as_list=True)
Provides operators and some other patterns in python functions way.
Imports:
from mongo_aggregation.patterns import regexand_({'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'}Provides aggregation operators and some other patterns in python functions way.
Imports:
from mongo_aggregation.aggr_patterns import merge_objects- $ifNull
There are two variations:
if_null('$doctor', {})
{'$ifNull': ['$doctor', {}]}
ifNull('$doctor', '')
{'$ifNull': ['$doctor', '']}Function looks after dollar prefix, so you can skip it:
if_null('doctor', {})
{'$ifNull': ['$doctor', {}]}- $in
There are two variations:
in_('doctor', 'doctors')
{'$in': ['$doctor', '$doctors']}merge_objects('$doctor', first_name='John', last_name='Doe')
# {'$mergeObjects': ['$doctor', {'first_name': 'John', 'last_name': 'Doe'}]}objfunction
Returns dictionary with specified fields as keys and values. For better understanding see usage examples below.
>>> obj('name,description')
{'name': '$name', 'description': '$description'}
>>> obj('name', 'description')
{'name': '$name', 'description': '$description'}
>>> obj('name,description', id='$code')
{'name': '$name', 'description': '$description', 'id': '$code'} - Fixed bug with multiple operators for a field in a
_convert_names_with_underlines_to_dotspattern.
- Added
in_aggregation pattern.
- Added
and_,and_nor,and_ormethods toMongoMatchFilter.
- Small fix.
- Added
if_nullpattern. - Removed
get_countmethod.countnow returns count and does not addcountstage. - Added
objpattern.
- Added
order_bymethod.
- Bug fixes.
.sortnow supports-emailsorting format (direction and field name by string).- Added
.replace_rootmethod. - Added
or_andand_logical operators._andis deprecated now. - Added
regexoperator pattern. - Added
merge_objectsaggregation operator (./aggr_patterns).
- Added
$addFieldsand$setstages. - Added
$filteroperator.