This is a library that generates programmatically aggregation queries for mongodb.
You can install library via setup file simple by:
python setup.py install
or via pip, in project folder you can run:
pip install aggregation_builder
Finally, you can just copy the aggregation_builder folder in your project and import the modules that you want.
For example the following aggregation query:
db.collection.aggregate(
[
{
'$group':
{
'_id': "$item",
'avgAmount': {'$avg': {'$multiply': ["$price", "$quantity"]}},
'avgQuantity': {'$avg': "$quantity"}
}
}
]
)
will be executed by libarary like:
aggregation_builder(collection).group(
id="$item",
avgAmount=AVG(MULTIPLY("$price", "$quantity")),
avgQuantity=AVG("$quantity")
).aggregate()
Moreover, if you decide to use MongoEngine library you can use AggregateQuerySet class that provides you the ability to execute aggregations via mongoengine model. For instance:
class Traffic(DynamicDocument):
meta = {'queryset_class': AggregateQuerySet}
Traffic.objects.aggregation_builder.group(id='response.data',first_obj=FIRST('$$ROOT')).skip(5).limit(7).execute()
This project is licensed under the MIT License - see the LICENSE file for details
-
The main purpose of this library is to handle mongo's aggregation framework. For this reason, whole library is based on aggregation framework faculties
-
This library is inspired by MongoEngine thus is created as an extra tool for this