Find Nodes by a Partial Path
Suppose we want to find a given node in a tree, given some path through a portion of the tree to that node, and then get back that node, and perhaps also everything below it.
With a materialized paths approach we can do the above. The main thing that needs tweaking is to make the operation fast if there is a path "a..b..c..d..e" to a document and we want to find documents with a path "..b..c..d..". If we are starting from the very top it is easy (and described above in the materialized paths section). However here we aren't starting at the top. One approach is to use a combination of materialized path plus an array of the node's ancestors, something like:
{ path : ",a,b,c,d,e,",
|
ancestor : ['a','b','c','d','e'] }
|
We could index on ancestors which will create a
[multikey|DOCS:Multikeys] index. Then we would do a query like the following to find nodes on path "...b,c,d..." with some efficiency:
find({ path : /,b,c,d,/, ancestor : 'd', <more_query_expressions_optionally> })
|
In the above the index on ancestor would be used and only docs from 'd' down need be inspected. The following could be tried which might be even better depending on how smart the query optimizer is:
find( { path : /,b,c,d,/, ancestor : { $all : ['a','d'] }, ... } )
|