Model Tree Structures with Child References

This page describes a data model that describes a tree-like structure in MongoDB documents by storing references in the parent-nodes to children nodes.

Pattern

The Child References 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 children.

Consider the following hierarchy of categories:

The following example models the tree using Child References, storing the reference to the node’s children in the field children:

mongo
db.categories.insert( { _id: "MongoDB", children: [] } )
db.categories.insert( { _id: "dbm", children: [] } )
db.categories.insert( { _id: "Databases", children: [ "MongoDB", "dbm" ] } )
db.categories.insert( { _id: "Languages", children: [] } )
db.categories.insert( { _id: "Programming", children: [ "Databases", "Languages" ] } )
db.categories.insert( { _id: "Books", children: [ "Programming" ] } )
db.categories.find()
{ "_id" : "MongoDB", "children" : [ ] }
{ "_id" : "dbm", "children" : [ ] }
{ "_id" : "Databases", "children" : [ "MongoDB", "dbm" ] }
{ "_id" : "Languages", "children" : [ ] }
{ "_id" : "Programming", "children" : [ "Databases", "Languages" ] }
{ "_id" : "Books", "children" : [ "Programming" ] }

The query to retrieve the immediate children of a node is fast and straightforward:

db.categories.findOne( { _id: "Databases" } ).children
[ "MongoDB", "dbm" ]

You can create an index on the field children to enable fast search by the child nodes:

db.categories.createIndex( { children: 1 } )
{
  "createdCollectionAutomatically": false,
  "numIndexesBefore": 1,
  "numIndexesAfter": 2,
  "ok": 1
}

You can query for a node in the children field to find its parent node as well as its siblings:

db.categories.find( { children: "MongoDB" } )
{ "_id": "Databases", "children": ["MongoDB", "dbm"] }

The Child References pattern provides a suitable solution to tree storage as long as no operations on subtrees are necessary. This pattern may also provide a suitable solution for storing graphs where a node may have multiple parents.

results matching ""

    No results matching ""