content_image/712473.jpg
<img src='content_image/712473.jpg'>

## Model Tree Structures with Parent References 

## 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 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 references (page 147) to “parent” nodes in children nodes. 

## Pattern 

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. 

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 : 

db.categories.insert( { _id: "MongoDB", parent: "Databases" } ) db.categories.insert( { _id: "dbm", parent: "Databases" } ) db.categories.insert( { _id: "Databases", parent: "Programming" } ) db.categories.insert( { _id: "Languages", parent: "Programming" } ) 

