HeWhoBuilds
HeWhoBuilds

Reputation: 97

MongoDB Tree with Array of Ancestors design

I have a question regarding how to design a tree of Ancestors in MongoDB.

For example if we have this:

{ "_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" }

This means we have a tree like this

       ACL      
        |       
      Apps      
     /    \     
   3222  1223   

I would like to have a node of "users" under each node. However, given that _id must be UNIQUE, I can't do so.

       ACL      
        |       
      Apps      
     /    \     
   3222  1223   
  /         \   
users      users

How would you go around this?

Edit: I have read the Model Tree information on MongoDB here: http://docs.mongodb.org/manual/tutorial/model-tree-structures/

Upvotes: 0

Views: 896

Answers (1)

KevinZhang
KevinZhang

Reputation: 61

I think there is no way to deal with your situation, unless you don't use "_id" as unique tree node, you can consider this schema:

{ "_id" : ["unique guid":xxx, "tree node": "users"], "ancestors" : [ ], "parent" : null }
{ "_id" : ["unique guid":xxx, "tree node": "users"], "ancestors" : [ "ACL" ], "parent" : "ACL" }

Upvotes: 1

Related Questions