db.categories.insert( { _id: "MongoDB", ancestors: [ "Books", "Programming", "Databases" ], db.categories.insert( { _id: "dbm", ancestors: [ "Books", "Programming", "Databases" ], db.categories.insert( { _id: "Databases", ancestors: [ "Books", "Programming" ], parent: db.categories.insert( { _id: "Languages", ancestors: [ "Books", "Programming" ], parent: db.categories.insert( { _id: "Programming", ancestors: [ "Books" ], parent: "Books" } ) db.categories.insert( { _id: "Books", ancestors: [ ], parent: null } ) db.categories.insert( { _id: "Books", path: null } ) db.categories.insert( { _id: "Programming", path: ",Books," } ) db.categories.insert( { _id: "Databases", path: ",Books,Programming," } ) db.categories.insert( { _id: "Languages", path: ",Books,Programming," } ) db.categories.insert( { _id: "MongoDB", path: ",Books,Programming,Databases," } ) db.categories.insert( { _id: "dbm", path: ",Books,Programming,Databases," } ) The following example models the tree using Array of Ancestors . In addition to the ancestors field, these docu- ments also store the reference to the immediate parent category in the parent field: MongoDB Documentation, Release 3.0.4 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 (page 161) pattern but is more straight- forward to use. 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 Concepts (page 145) 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 by storing full relationship paths between documents. 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: 161 4.3. Data Model Examples and Patterns Model Tree Structures with Materialized Paths Overview Pattern • 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 field ancestors to find all its descendants: db.categories.findOne( { _id: "MongoDB" } ).ancestors db.categories.createIndex( { ancestors: 1 } ) db.categories.find( { ancestors: "Programming" } )