Skip to main content

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

Metadata

Release files for PyMongoAgg 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyMongoAgg 0.0.2
File Size Uploaded
PyMongoAgg-0.0.2.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PyMongoAgg 0.0.2
File Interpreter ABI Platform
PyMongoAgg-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / PyMongoAgg-0.0.2.tar.gz

Download URL PyMongoAgg-0.0.2.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c5bad3f00aabbabfed58aabf11f2e724a7ebaf29905ec73aaaf189767c2e35db
BLAKE2b-256 checksum
How to use checksums
b013b57aa45fb1fbc0b9e65066a049722a186ef451fa81cb124074978c86c931
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / PyMongoAgg-0.0.2-py3-none-any.whl

Download URL PyMongoAgg-0.0.2-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1cd4d6854b82ae1c09c24dc331c92beb67c53901c29656762e55536be43451fd
BLAKE2b-256 checksum
How to use checksums
bf0858a0801925c53f2b5f8512e93cb0a7bac5bef92e4a7388f5586f14517908
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page