Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine root objects in an arangodb tree graph?

Tags:

arangodb

I have a document collection containing tree nodes and an edge collection containing "is child of" like this:

Folders=[
{_key:"1",name:"Root1"},
{_key:"2",name:"Root2"},
{_key:"3",name:"Root1.Node1"},
{_key:"4",name:"Root1.Node2"}]

FolderRelations=[
{_from:"Folders/3",_to:"Folders/1"},
{_from:"Folders/4",_to:"Folders/1"}
]

Now I would like to determine which Folder items are root objects in that tree (all objects that have no outbound relation).

Maybe, I am a bit stuck in thinking SQL, I would like to carry out something like:

SELECT * 
FROM Folders 
WHERE NOT EXIST (SELECT * FROM FolderRelations WHERE FolderRelations.FromKey=Folders.Key)

For using the traversal and path functionality, I have no vertex to start with.

like image 531
BurnedFeather Avatar asked Jan 19 '14 16:01

BurnedFeather


1 Answers

here is an AQL example that should solve your problem:

for f in Folders
filter LENGTH( EDGES(FolderRelations, v._id, "outbound")) == 0
return f

you will get a list of all vertices that have no folder above in the hierarchy.

but be aware: saving {key:1} will not have the desired effect, you have to set:

{_key: "1"}

_key is used for internal key attribute, and it has to be a string.

like image 136
mchacki Avatar answered Jan 03 '23 12:01

mchacki