Model Tree Structures with Parent References
来源:互联网 发布:大数据设计哪些技术 编辑:程序博客网 时间:2024/05/21 00:19
Model Tree Structures with Parent 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 storingreferences to “parent” nodes in children nodes.
Pattern
The Parent References pattern stores each tree node in a document; in addition to the tree node, the document stores the id of the node’s parent.
Consider the following hierarchy of categories:
The following example models the tree using Parent References, storing the reference to the parent category in the field parent
:
db.categories.insert( { _id: "MongoDB", parent: "Databases" } )db.categories.insert( { _id: "dbm", parent: "Databases" } )db.categories.insert( { _id: "Databases", parent: "Programming" } )db.categories.insert( { _id: "Languages", parent: "Programming" } )db.categories.insert( { _id: "Programming", parent: "Books" } )db.categories.insert( { _id: "Books", parent: null } )
The query to retrieve the parent of a node is fast and straightforward:
db.categories.findOne( { _id: "MongoDB" } ).parent
You can create an index on the field
parent
to enable fast search by the parent node:db.categories.createIndex( { parent: 1 } )
You can query by the
parent
field to find its immediate children nodes:db.categories.find( { parent: "Databases" } )
The Parent Links pattern provides a simple solution to tree storage but requires multiple queries to retrieve subtrees.
- Model Tree Structures with Parent References
- Model Tree Structures with Child 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
- MyTreeSet(with parent node)
- 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
- OpenSessionInViewFilter源码分析
- gcc版本查看
- 后台秒杀架构设计与实现(一)
- 小猪的Python学习之旅 —— 1.基础知识储备
- jsEnter键的屏蔽
- Model Tree Structures with Parent References
- 《Apache Flume教程》第一章 Flume 的简介
- GR/IR与MR11的使用
- Jlink工具下的RTT调试应用
- 第一次,不敢发表太多
- word_embedding的负采样算法,Negative Sampling 模型
- 两个HTML页面之间传值
- AMOS分析技术:模型的群组分析;AMOS如何对同一个模型分别用男性数据和女性数据拟合?
- ubuntu 16.04安装 navicat