On this page本页内容
This page describes a data model that describes a tree-like structure in MongoDB documents by storing references to “parent” nodes in children nodes.本页描述了一个数据模型,该模型通过在子节点中存储对“父”节点的引用来描述MongoDB文档中的树状结构。
The Parent References pattern stores each tree node in a document; in addition to the tree node, the document stores the id of the node’s parent.父引用模式将每个树节点存储在文档中;除了树节点外,文档还存储节点的父节点的id。
Consider the following hierarchy of categories:考虑以下类别的层次结构:
The following example models the tree using Parent References, storing the reference to the parent category in the field 以下示例使用父引用对树进行建模,并将对父类别的引用存储在“父”字段中:parent
:
parent
to enable fast search by the parent node:parent
字段上创建索引,以启用父节点的快速搜索:
parent
field to find its immediate children nodes:parent
字段进行查询,以查找其直接子节点:
$graphLookup
.$graphLookup
。