- Core MongoDB Operations (CRUD) >
- Model Tree Structures with an Array of Ancestors
Model Tree Structures with an Array of Ancestors¶
Overview¶
Data in MongoDB has a flexible schema. Collections do not enforce document structure. Decisions that affect how you model data can affect application performance and database capacity. See Data Modeling Considerations for MongoDB Applications for a full high level overview of data modeling in MongoDB.
This document 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.
Pattern¶
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.
Consider the following example that models a tree of categories using Array of Ancestors:
The query to retrieve the ancestors or path of a node is fast and straightforward:
You can create an index on the field
ancestors
to enable fast search by the ancestors nodes:You can query by the
ancestors
to find all its descendants:
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.