Model Tree Structures with Materialized Paths

Model Tree Structures with Materialized Paths

On this page

Overview

Data in MongoDB has a flexible schemaCollections do not enforce document structure. Decisions that affect how you model data can affect application performance and database capacity. See Data Modeling Conceptsfor 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.

Pattern

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:

Tree data model for a sample 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:

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," } )
  • You can query to retrieve the whole tree, sorting by the field path:

    db.categories.find().sort( { path: 1 } )
    
  • You can use regular expressions on the path field to find the descendants of Programming:

    db.categories.find( { path: /,Programming,/ } )
    
  • You can also retrieve the descendants of Books where the Books is also at the topmost level of the hierarchy:

    db.categories.find( { path: /^,Books,/ } )
    
  • To create an index on the field path use the following invocation:

    db.categories.createIndex( { path: 1 } )
    

    This index may improve performance depending on the query:

    • For queries from the root Books sub-tree (e.g. /^,Books,/ or /^,Books,Programming,/), an index on the path field improves the query performance significantly.

    • For queries of sub-trees where the path from the root is not provided in the query (e.g. /,Databases,/), or similar queries of sub-trees, where the node might be in the middle of the indexed string, the query must inspect the entire index.

      For these queries an index may provide some performance improvement if the index is significantly smaller than the entire collection.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值