aggregation_builder

Package that implements a simple mongodb aggregation builder.


Keywords
mongodb
License
MIT-feh
Install
pip install aggregation_builder==0.0.4

Documentation

aggregation_builder

This is a library that generates programmatically aggregation queries for mongodb.

Installing

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.

Examples

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()

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • 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