3 comments

[ 3.1 ms ] story [ 12.2 ms ] thread
Is it possible to reliably do this in reverse?

A friend of mine built an app using aggregations - they are hard to work with and understand imho, I'd rather convert them to simpler Python operations. Fetch some rows, join them to some rows from another table, etc.

This is definitely possible to do in reverse. Unfortunately, the thing that makes the Python-MongoDB translation so easy is that Python provides utilities to parse and lex the Python source code. If you were to translate from MongoDB back to Python, you would have to construct the AST yourself. You can use `ast.unparse` to convert a Python AST into Python source code. The main issue that I find with aggregations is that they are prefix rather than infix notation. Converting them to infix perhaps could make them easier to understand as well?
Hmm, yeah, there is probably some C++ code in the mongodb source tree you could use to parse the aggregations. This whole app needs a refactor anyway - probably just live with it until then.