Storing Directory Hierarchy in a Key-Value Data store

前端 未结 4 1416
一个人的身影
一个人的身影 2020-12-12 12:57

What is a clean/efficient method for storing the directory Hierarchy/tree in a Key-Value database (in my case MongoDB but any of them)?

For example a tree structure

4条回答
  •  暖寄归人
    2020-12-12 13:50

    The method you currently use now is called adjacency list model.

    Another model to store hierarchical data in a (relational) database is the nested set model. Its implementation in SQL databases is well known. Also see this article for the modified preorder tree traversal algorithm.

    A very simple method: you could store a path per object - with those it should be easy to query trees in NOSQL databases:

    { path: "Color", ... }
    { path: "Color.Red", ... }
    { path: "Color.Red.Apple", ... }
    { path: "Color.Red.Cherry", ... }
    

    When nodes will be removed or renamed some paths must be updated. But in general, this method looks promising. You just have to reserve a special character as separator. The storage space overhead should be negligible.

    edit: this method is called materialized path

    Finally, here is a comparison of different methods for hierarchical data in NOSQL databases.

提交回复
热议问题