So, if I understood the question correctly, you want
- find the given node in the tree, given some way through the part of the tree to this node plus an additional expression query.
- then return node and everything under it.
With materialized paths, you can do this. The main thing that you need to configure if the document has the path "a..b..c..d..e" and you want to find documents using the path "..b..c..d ..", as do it fast. If we start from the top, it's easy. However, we are not here. It might make sense to use a combined approach when the document has a materialized path for node plus an array of node ancestors, something like:
{ path : ",a,b,c,d,e,", ancestor : ['a','b','c','d','e'] }
We could index the ancestors that would create the multicode index. Then we execute the following query: find the nodes on the path "... b, c, d ..." with some efficiency:
find( { path : /,b,c,d,/, ancestor : 'd', <more_query_expressions_optionally> } )
In the above example, the index for the ancestor will be used, and only inspections with dd need to be checked. One could try the following: it could be even better, depending on how smart the query optimizer is:
find( { path : /,b,c,d,/, ancestor : { $all : ['a','d'] }, ... } )