2015-02-26 39 views
0

我正在使用MongoDB 2.6.7,我在MongoDB mode tree structure with array of ancestors之後創建了類別集合,但這個模型真的過於複雜,因此我想知道是否有人可以請最好的模型/架構設計,以便遵循以最好地實現我的分類收集需求,內容如下:類別集合的Mongodb模式建議

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 

我當前類別集設計如下:

{ _id: "", 
    ancestors: [ ], 
    parent: "" 
} 

感謝您的幫助。

回答

1

添加一個字段無論是直接的兒童或者只是爲了表明,有孩子:

{ 
    _id: "", 
    ancestors: [<_id's>], 
    "children" : [<_id's>] 
} 

我不知道什麼樣的parent的目的。這看起來像是一個很好的A-C架構設計。 D是應用程序需要執行的東西。 E對於您的設計來說也不算太糟糕,它只是涉及爲正在重新部署的根節點的所有子節點更新祖先。 B和E互相爭用 - 如果通過將它們存儲在節點上來快速瞭解祖先,則需要在重新定位子樹時更新所有子節點。 B最有可能比E更普遍,所以對於繁瑣的E來說,快B的權衡是合理的。

+0

感謝您的幫助。因爲我可能會更新很多文件來更新祖先,所以在樹中重新分配特定節點時,子組會進行數組更新,或者在執行操作時鎖定整個集合,或者執行類似原子更新的操作以防止出現可能的問題如果另一個應用程序用戶在應用程序仍在更新重新分配的節點的祖先/孩子時嘗試更新樹? 在一個小問題上,我明白db.collection.update()在單個文檔上是原子的,所以我指的是在幾個文檔更新上處於原子狀態。謝謝 – MChan