On this page本页内容
This page describes a data model that describes a tree-like structure in MongoDB documents by storing full relationship paths between documents.本页描述了一个数据模型,该模型通过存储文档之间的完整关系路径来描述MongoDB文档中的树状结构。
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. 物化路径模式将每个树节点存储在文档中;除了树节点外,文档还以字符串形式存储节点祖先或路径的id。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:path
中;路径字符串使用逗号,
作为分隔符:
path
:path
排序:
path
field to find the descendants of Programming
:path
字段中使用正则表达式来查找Programming
的后代:
Books
where the Books
is also at the topmost level of the hierarchy:Books
的后代,其中Books
也位于层次结构的最顶层:
path
use the following invocation:path
上创建索引,请使用以下调用:
This index may improve performance depending on the query:根据查询的不同,此索引可能会提高性能:
Books
sub-tree (e.g. /^,Books,/
or /^,Books,Programming,/
), an index on the path
field improves the query performance significantly.Books
子树(例如/^,Books,/
或/^,Books,Programming,/
)的查询,路径字段上的索引可以显著提高查询性能。/,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./,Databases,/
)中没有从根中提供路径,或者子节点类似的查询,其中节点可能位于索引字符串的中间,查询必须检查整个索引。
For these queries an index may provide some performance improvement if the index is significantly smaller than the entire collection.对于这些查询,如果索引明显小于整个集合,则索引可能会提供一些性能改进。