我使用的是MongoDB 2.6.7,我在MongoDB mode tree structure with array of ancestors之后创建了Categories集合,但是这个模型对我来说太复杂了,所以我想知道是否有人可以建议我最好的模型/模式设计来遵循,以便最好地实现我的Categories集合需求,如下所示:
A. Build a tree with unlimited levels of categories and sub-categories
B. For any node in the tree I can easily know the list of this node ancestors
C. For any node in the tree I can easily know if it has any child nodes or not
D. I can only create sub-nodes (sub-categories) to those categories not referenced by any item in the inventory
E. I can move any sub-node in the tree along with its child-nodes to a different node, sod for example if i have:
- 1
--11
---111
---112
---113
-----1131
-----1132
-----1133
--12
---121
---122
---123
so I can move node 113 with its sub-children 1131, 1132, 1133 and place 113 under 123 so that it will look like this:
- 1
--11
---111
---112
--12
---121
---122
---123
-----113
--------1131
--------1132
--------1133我目前的categories集合设计如下:
{ _id: "",
ancestors: [ ],
parent: ""
}谢谢你的帮助。
发布于 2015-02-27 00:57:30
添加一个字段,用于直接的子项,或者只是为了指示有子项:
{
_id: "",
ancestors: [<_id's>],
"children" : [<_id's>]
}我不确定parent的目的是什么。这看起来是一个很好的A-C模式设计。D是应用程序需要强制执行的内容。E对于您的设计来说也不是那么糟糕,它只涉及更新正在重新定位的根节点的所有子节点的祖先。B和E相互争用-如果您通过将祖先存储在节点上来快速了解祖先,则在重新定位子树时需要更新所有子树。B很可能比E更常见,因此快速B与繁琐的E之间的权衡是合理的。
https://stackoverflow.com/questions/28740111
复制相似问题