[MongoDB]-document-based database design patterns-How to store tree data

Source: Internet
Author: User
Tags mongodb

Storing tree-structured data in a database is a very common requirement, typically such as the forum system's relationship. In the traditional relational database, a variety of solutions have been produced.

This paper describes several design patterns of using relational database and document type database as storage in order to store tree structure data as requirement. A. Relational database design Pattern 1

ID name parent_id
1 A Null
2 B 1
3 C 1
4 D 2

The above illustration shows one of the traditional design methods, which is to store each node of the tree structure as a row in a relational database, and each node holds a pointer to its parent node. Advantages : Easy to understand the structure, insert the modification operation is very simple disadvantage : If you want to get all the sub-nodes of a node, it will be a very disgusting thing B. relational database design pattern 2

ID name parent_id Left Right
1 A Null 1 8
2 B 1 2 5
3 C 1 6 7
4 D 2 3 4

The above figure on the basis of pattern 1 more than two columns, left and right, the equivalent of the btree in the branches, respectively, store the left and right branch nodes of the maximum and minimum values. Advantage : To find a node's sub-node is very easy, only need to do a range of queries on the line (such as Node B nodes, only need to query ID >=2 && id<=5) disadvantage : Because the tree structure exists in this area, So adding or modifying existing nodes can have a ripple effect, and the operation is too complex C. Document Database design Pattern 1

{"
  name": "A",
  "Children": [
    {"name": "B", "children": [{"Name": "D"}]},
    {"name": "C"}
  ]
}

Save the entire tree structure as a document, the structure of the document is tree-like structure, simple and easy to understand. Pros : Easy to understand disadvantages : The document will become larger, the changes to all nodes are concentrated in this document, concurrent Operation Limited D. Document Database design pattern 2

{"_id": "A", "Children": ["B", "C"]}
{"_id": "B", "Children": ["D"]}
{"_id": "C"}
{"_id": "D"}

Save all the child nodes of each node advantages : Simple structure, convenient to find sub-nodes: Finding The parent node can be troublesome E. Document database design Patterns 3

{"
  leaf": "A",
  "Children": [
    {"leaf": "B", "children": [{"Leaf": "D"}]},
    {"leaf": "C"}
  ]
}
{"_id": "A", ...}
{"_id": "B", ...}
{"_id": "C", ...}
{"_id": "D", ...}

Leveraging the advantages of document-based storage schema-less, first use the above C scheme to store a large tree document, and then store the additional information for each node separately. Advantages : Easy to operate, the structure of the operation can directly manipulate the large tree document, the operation of the data only need to operate a single data disadvantage : All nodes are modified to focus on this document, concurrent operation is limited

English original link: Modeling a Tree in a Document Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.