On this page本页内容
This page describes a data model that describes a tree-like structure in MongoDB documents using references to parent nodes and an array that stores all ancestors.本页描述了一个数据模型,该模型使用对父节点的引用和存储所有祖先的数组来描述MongoDB文档中的树状结构。
The Array of Ancestors pattern stores each tree node in a document; in addition to the tree node, document stores in an array the id(s) of the node’s ancestors or path.祖先数组模式将每个树节点存储在文档中;除了树节点外,文档还将节点的祖先或路径的id存储在一个数组中。
Consider the following hierarchy of categories:考虑以下类别的层次结构:
The following example models the tree using Array of Ancestors. 下面的示例使用祖先数组对树进行建模。In addition to the 除了ancestors
field, these documents also store the reference to the immediate parent category in the parent
field:ancestors
字段外,这些文档还在父字段中存储对直接父类别的引用:
ancestors
to enable fast search by the ancestors nodes:ancestors
字段上创建索引,以启用“祖先”节点的快速搜索:
ancestors
to find all its descendants:ancestors
进行查询,以查找其所有后代:
The Array of Ancestors pattern provides a fast and efficient solution to find the descendants and the ancestors of a node by creating an index on the elements of the ancestors field. 祖先数组模式提供了一种快速高效的解决方案,通过在祖先字段的元素上创建索引来查找节点的后代和祖先。This makes Array of Ancestors a good choice for working with subtrees.这使得祖先数组成为处理子树的好选择。
The Array of Ancestors pattern is slightly slower than the Materialized Paths pattern but is more straightforward to use.祖先数组模式略慢于物化路径模式,但更易于使用。