Model Tree Structures with Materialized Paths
来源:互联网 发布:sql 身份证导出 编辑:程序博客网 时间:2024/06/06 18:45
Model Tree Structures with Materialized Paths
On this page
- Overview
- Pattern
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 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:
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 ofProgramming
:db.categories.find( { path: /,Programming,/ } )
You can also retrieve the descendants of
Books
where theBooks
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 thepath
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.
- Model Tree Structures with Materialized Paths
- Model Tree Structures with Parent References
- Model Tree Structures with Child References
- Model Tree Structures with Nested Sets
- Model Tree Structures with an Array of Ancestors
- No. 04 - Paths with Specified Sum in Binary Tree
- Hackerrank-Data structures-Tree
- Requirements for Materialized Views with Aggregates
- Working with Packing Structures -----MSDN
- Getting Started with Data Structures
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(6-1)——Tree
- [leetcode][tree] Binary Tree Paths
- Print tree paths
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- SpirngMVC系统启动系统配置初始化执行顺序以及防止onApplicationEvent执行两次
- Python selenium 后台运行模拟登录操作(三)
- 商业规则引擎和开源规则引擎的测试对比
- spring quartz(定时器)
- 《西遊記後傳-挂機版》隐私政策
- Model Tree Structures with Materialized Paths
- 禁止td内容不换行
- 流程中心使用详情(二)表单控件
- Android studio 一键国际化翻译插件最新版
- 机器学习之决策树 Decision Tree(三)scikit-learn算法库
- 物质属性
- 接口开发总结
- 第十二周项目二——Kruskal算法的验证
- 关于android系统添加apn