Skip to main content

A Python-MongoDB Aggregation transpiler.

Project description

PyMongoAgg

Minimal POC of a Python-MongoDB Aggregation transpiler.

Have you ever, like me, had to write a MongoDB aggregation expression, and found that you would rather put your head underneath a car tire and ask the driver to do a burnout? Well, this is the solution for you. Now you can write your aggregation pipelines using native Python syntax, and PyMongoAgg will handle conversion by parsing the AST of your function.

This was originally a Skunkworks project (MongoDB's internal hackathon), however I was let go summarily only 2 weeks after my project got to the finals. Luckily, the project has a very permissive license, and I will be continuing development work by myself.

The version that MongoDB turned into an official repository is not maintained: https://github.com/mongodb-labs/PyMongoAgg

Example:

def basic_func():
    y = a
    a = (a + b) / 2
    b = sqrt(b * y)
    t = t - (x * (y - a) ** 2)
    x = x * 2
    ...
output_dict = transpile_function(basic_func)
print(output_dict)
[{'$set': {'y': '$a'}}, 
 {'$set': {'a': {'$divide': [{'$add': ['$a', '$b']}, 2]}}},
 {'$set': {'b': {'$sqrt': [{'$multiply': ['$b', '$y']}]}}},
 {'$set': {'t': {'$subtract': ['$t', {'$multiply': ['$x', {'$pow': [{'$subtract': ['$y', '$a']}, 2]}]}]}}},
 {'$set': {'x': {'$multiply': ['$x', 2]}}}]

It also supports boolean operations!

def bool_func(y, a, b, c):
    y = 1
    a = (y and 0) and 1
    b = y or 0
    c = not y
    ...
[{'$set': {'y': 1}}, 
 {'$set': {'a': {'$and': [{'$and': ['$y', 0]}, 1]}}}, 
 {'$set': {'b': {'$or': ['$y', 0]}}},
 {'$set': {'c': {'$not': ['$y']}}}]

Changes since original Skunkworks presentation:

  • Added support for builtins like sqrt
  • Added support for boolean operations
  • Added tests that run the transpiled functions and checks the result of running the functions against the result of executing the transpiled MongoDB aggregation.

To-do:

  • Control flow
  • Vector operations
  • More aggregation operators

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

PyMongoAgg-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyMongoAgg-0.0.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file PyMongoAgg-0.0.2.tar.gz.

File metadata

  • Download URL: PyMongoAgg-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for PyMongoAgg-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c5bad3f00aabbabfed58aabf11f2e724a7ebaf29905ec73aaaf189767c2e35db
MD5 edf67ff9c68468e807ae29b1da4f0bf1
BLAKE2b-256 b013b57aa45fb1fbc0b9e65066a049722a186ef451fa81cb124074978c86c931

See more details on using hashes here.

File details

Details for the file PyMongoAgg-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: PyMongoAgg-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for PyMongoAgg-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1cd4d6854b82ae1c09c24dc331c92beb67c53901c29656762e55536be43451fd
MD5 bb98094f26deb9bd76daef7275c75243
BLAKE2b-256 bf0858a0801925c53f2b5f8512e93cb0a7bac5bef92e4a7388f5586f14517908

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page