Model Tree Structures with Nested Sets
来源:互联网 发布:电脑智能机器人软件 编辑:程序博客网 时间:2024/06/04 18:36
Model Tree Structures with Nested Sets
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 that optimizes discovering subtrees at the expense of tree mutability.
Pattern
The Nested Sets pattern identifies each node in the tree as stops in a round-trip traversal of the tree. The application visits each node in the tree twice; first during the initial trip, and second during the return trip. The Nested Sets pattern stores each tree node in a document; in addition to the tree node, document stores the id of node’s parent, the node’s initial stop in the left
field, and its return stop in the right
field.
Consider the following hierarchy of categories:
The following example models the tree using Nested Sets:
db.categories.insert( { _id: "Books", parent: 0, left: 1, right: 12 } )db.categories.insert( { _id: "Programming", parent: "Books", left: 2, right: 11 } )db.categories.insert( { _id: "Languages", parent: "Programming", left: 3, right: 4 } )db.categories.insert( { _id: "Databases", parent: "Programming", left: 5, right: 10 } )db.categories.insert( { _id: "MongoDB", parent: "Databases", left: 6, right: 7 } )db.categories.insert( { _id: "dbm", parent: "Databases", left: 8, right: 9 } )
You can query to retrieve the descendants of a node:
var databaseCategory = db.categories.findOne( { _id: "Databases" } );db.categories.find( { left: { $gt: databaseCategory.left }, right: { $lt: databaseCategory.right } } );
The Nested Sets pattern provides a fast and efficient solution for finding subtrees but is inefficient for modifying the tree structure. As such, this pattern is best for static trees that do not change.
- Model Tree Structures with Nested Sets
- Model Tree Structures with Parent References
- Model Tree Structures with Child References
- Model Tree Structures with Materialized Paths
- Model Tree Structures with an Array of Ancestors
- 关于nested sets树
- The Nested Set Model
- Hackerrank-Data structures-Tree
- The Nested Set Model 算法
- Working with Packing Structures -----MSDN
- Getting Started with Data Structures
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(6-1)——Tree
- 【索引】 Advanced Data Structures :: Segment Tree
- B+Tree index structures in InnoDB
- Data Structures with Java—table(1)
- Data Structures & Problem Solving with C++
- 读书笔记之《Data Structures With C++》
- QT Tree model
- 遍历list时删除元素的正确做法
- 关于HTTP
- QT控件大全 二十八 QMultiYearCalender
- 队列操作的实现-链式存储
- LintCode 第423题
- Model Tree Structures with Nested Sets
- 阿里云前端周刊
- 2018年才是人工智能元年?今年不是!
- mysql大表优化方案
- 理解朴素贝叶斯
- LinearLayout之weight完美详解
- ftp文件上传和下载
- spring boot-application.properties配置文件属性
- Sublime Text 3 build 3143 LICENSE