首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类别集合的Mongodb模式建议

类别集合的Mongodb模式建议
EN

Stack Overflow用户
提问于 2015-02-26 18:35:48
回答 1查看 230关注 0票数 0

我使用的是MongoDB 2.6.7,我在MongoDB mode tree structure with array of ancestors之后创建了Categories集合,但是这个模型对我来说太复杂了,所以我想知道是否有人可以建议我最好的模型/模式设计来遵循,以便最好地实现我的Categories集合需求,如下所示:

代码语言:javascript
复制
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集合设计如下:

代码语言:javascript
复制
{ _id: "", 
  ancestors: [ ], 
  parent: "" 
}

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-02-27 00:57:30

添加一个字段,用于直接的子项,或者只是为了指示有子项:

代码语言:javascript
复制
{
    _id: "", 
    ancestors: [<_id's>], 
    "children" : [<_id's>]
}

我不确定parent的目的是什么。这看起来是一个很好的A-C模式设计。D是应用程序需要强制执行的内容。E对于您的设计来说也不是那么糟糕,它只涉及更新正在重新定位的根节点的所有子节点的祖先。B和E相互争用-如果您通过将祖先存储在节点上来快速了解祖先,则在重新定位子树时需要更新所有子树。B很可能比E更常见,因此快速B与繁琐的E之间的权衡是合理的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28740111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档