0

我有一个关于如何在 MongoDB 中设计祖先树的问题。

例如,如果我们有这个:

{ "_id" : "ACL", "ancestors" : [ ], "parent" : null }
{ "_id" : "apps", "ancestors" : [ "ACL" ], "parent" : "ACL" }
{ "_id" : "3222", "ancestors" : [ "ACL", "apps" ], "parent" : "apps" }
{ "_id" : "1223", "ancestors" : [ "ACL", "apps" ], "parent" : "apps" }

这意味着我们有这样一棵树

       ACL      
        |       
      Apps      
     /    \     
   3222  1223   

我想在每个节点下都有一个“用户”节点。但是,鉴于 _id 必须是唯一的,我不能这样做。

       ACL      
        |       
      Apps      
     /    \     
   3222  1223   
  /         \   
users      users

你会如何解决这个问题?

编辑:我在这里阅读了关于 MongoDB 的模型树信息:http: //docs.mongodb.org/manual/tutorial/model-tree-structures/

4

1 回答 1

1

我认为没有办法处理你的情况,除非你不使用“_id”作为唯一的树节点,你可以考虑这个模式:

{ "_id" : ["unique guid":xxx, "tree node": "users"], "ancestors" : [ ], "parent" : null }
{ "_id" : ["unique guid":xxx, "tree node": "users"], "ancestors" : [ "ACL" ], "parent" : "ACL" }
于 2013-08-23T09:39:53.657 回答