- Data Models >
- Data Model Examples and Patterns >
- Model Tree Structures >
- Model Tree Structures with Materialized Paths
Model Tree Structures with Materialized Paths¶
Overview¶
This page describes a data model that describes a tree-like structure in MongoDB documents by storing full relationship paths between documents.
Pattern¶
The Materialized Paths pattern stores each tree node in a document; in addition to the tree node, document stores as a string the id(s) of the node’s ancestors or path. Although the Materialized Paths pattern requires additional steps of working with strings and regular expressions, the pattern also provides more flexibility in working with the path, such as finding nodes by partial paths.
Consider the following hierarchy of categories:
The following example models the tree using Materialized Paths,
storing the path in the field path
; the path string uses the comma
,
as a delimiter:
You can query to retrieve the whole tree, sorting by the field
path
:You can use regular expressions on the
path
field to find the descendants ofProgramming
:You can also retrieve the descendants of
Books
where theBooks
is also at the topmost level of the hierarchy:To create an index on the field
path
use the following invocation:This index may improve performance depending on the query:
For queries from the root
Books
sub-tree (e.g./^,Books,/
or/^,Books,Programming,/
), an index on thepath
field improves the query performance significantly.For queries of sub-trees where the path from the root is not provided in the query (e.g.
/,Databases,/
), or similar queries of sub-trees, where the node might be in the middle of the indexed string, the query must inspect the entire index.For these queries an index may provide some performance improvement if the index is significantly smaller than the entire collection.