Using Materialized Path Trees with Python

Hi guys,

I recently asked about this in another community and got some very kind answers, but the problem isn’t solved. They referred me to go in this category to get more viewers. in case you wanna have a look at the original question, the link’s in the comments.

The problem is, the docs of mongo in the section for Materialized Path Trees are not translated for Python, only JavaScript, which I’m not familar with yet. I guess maybe I just have to learn basic JS to use this feature.

I’d like to store data like in this example : [{ "_id": "Programming", "path": ",Books," }, { "_id": "Databases", "path": ",Books,Programming," }]
and with a simple query find all the nodes that are ascending from the path Books: db.categories.find( { path: "^,Books," } ). Note that I in this example already changed the syntax of mongo, as it is written for JS and I’m using Python. Of course, I don’t get any output. Python doesn’t know, what I want from it. It seems like, the features I’m asking for either have a different syntax or don’t even exist for Python.

The guys commenting under my original question suggested to use the $regex operator in Python, following this part of the docs: The problem is though, that this doesn’t help with Materialized Path Trees. It only makes it possible to query data and find all the nodes, which have the asked string, like in this case Books, but that means it also for example would find nodes with the path Bookstore. I mean there’s a clean feature to solve this using JS, so I guess I’m just learning this now. Though I would be more than happy if there would be a way to use this feature in Python.

Btw. here’s the link to the section of the docs about Materialized Path Trees (which I need translated for Python, if possible). I have already asked this in the customer support, but they referred me to the community as they don’t cover this depht of topics apparently.


Link to original question: Is it possible to use materialized path trees with python?

What do you want to do that you cannot do with $regex?

The query { "path" : { "$regex" : "^,Books," } }, almost verbatim from the other thread, is valid Python and should matches both _id:Programming and _id:Databases.

: steevej@rpi ; python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> query = { "path" : { "$regex" : "^,Books," } }
>>> query
{'path': {'$regex': '^,Books,'}}

If not, then we are missing some pieces of the puzzle that might be resolve if your share your Python.

Hi Moritz,

I’m also a Python programmer! You’ve already received the correct answer from a number of people - use the $regex operator, but I wanted to clarify why this is the correct advice:

The JavaScript code you’ve seen in our docs is here:

db.categories.find( { path: /^,Books,/ } )

It looks a bit like Python, because they’re quite similar languages, but I wanted to extract one part of it, which is: /^,Books,/. This looks a bit like a string, but it isn’t - it’s a compiled regular expression in JavaScript. It’s the equivalent to the following in Python: re.compile("^,Books,") - which returns a regular expression object.

What I’m trying to highlight here is that the example code is already using a regular expression. The JavaScript driver does something quite clever with this regular expression object - it automatically converts it to the following MongoDB query expression: { "$regex": "^,Books," }. As far as I know, the Python driver doesn’t do the same thing with Python regular expression objects, so you need to supply { "$regex": "^,Books," } instead of a Python-native regular expression object.

Your question above stated the following:

… but that’s not actually the case. The regular expression ^,Books, will only find paths that begin with ,Books, - including those commas. So it would match ,Books,tore, but not Bookstore.

It’s worth reading up on regular expressions, as they’re very powerful for text matching, and you can build expressions that will very specifically match exactly what you’re looking for. The Python Regex Docs are very good for the basics, but I also highly recommend the O’Reilly Regex book if you really want to become an expert.

I hope this helps,



this is so nice, thanks Mark!

I must have made a mistake, when trying to check if it would find Bookstore as well.

Thank you so much.


No problem, @Moritz_Honscheidt. Any time!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.