Model Tree Structures with Child References
来源:互联网 发布:java poi api 编辑:程序博客网 时间:2024/06/03 12:14
Model Tree Structures with Child References
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 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
:
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" ] } )
The query to retrieve the immediate children of a node is fast and straightforward:
db.categories.findOne( { _id: "Databases" } ).children
You can create an index on the field
children
to enable fast search by the child nodes:db.categories.createIndex( { children: 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" } )
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.
- Model Tree Structures with Child References
- Model Tree Structures with Parent References
- Model Tree Structures with Materialized Paths
- Model Tree Structures with Nested Sets
- Model Tree Structures with an Array of Ancestors
- Model One-to-Many Relationships with Document References
- Ant:Path-like Structures和References
- Hackerrank-Data structures-Tree
- 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++》
- iOS 9: Staying Organized with Storyboard References
- QT Tree model
- 国外比较乱套……
- CardView
- 简单的java图片存储方法
- 微信小程序、流应用、原生应用app、轻应用
- 前端跨域调用--JSONP
- Model Tree Structures with Child References
- javascript: instanceof 和 typeof 的区别
- clgao资源网址
- Java面试题全集(下)
- python多进程
- Java环境变量设置
- JsonP的优化
- 连接字符串记录
- 算法训练 反置数