2

ArangoDB is great for retrieving a graph of data. A tree is a kind of graph.
We want to store data of buildings in a database. The structure of a building can be thought of as a hierarchy of parts.
The data structure is very simple: assemblies containing other assemblies, unlimited levels deep. Assemblies are stored as documents, and linked by an edge collection contains.

The data that I need, can be fetched with

FOR v, e, path IN 1..3 OUTBOUND
'assemblies/14810426' contains
RETURN path

The data is there, however, I want to have a json like

{name: "Office Building", 
 assemblies: [{name: "Director's office",
   assemblies: [{name: "Chair"}, {name: "Desk"}]}, 
{name: "Toilet}]}

Is there a way to get this format straight from ArangoDB, or do I need to write code myself to convert the graph path to a tree?
I guess writing a Foxx service is an option, but I would like to avoid that.

There is a similar question, with answer, but that was for the previous version of ArangoDB. It requires a bit of foxx code. I was hoping to have a solution, straight from AQL.

2 Answers2

2

You could make use of subquery to aggregate results and add it to your return result.

AQL Query like this

FOR doc IN assemblies 
FILTER doc._id=='assemblies/14810426'
LET conn_docs=(
  FOR v,e,path IN 1..3 
    OUTBOUND doc contains RETURN {'name':v.name}
) 
RETURN {'name':doc.name,'assemblies':conn_docs}

would do the task, which aggregates the connected assemblies to a query doc and place those in the return result

Brian Burns
  • 14,953
  • 5
  • 69
  • 59
kavin
  • 86
  • 6
  • 1
    This gives me one level of the hierarchy. I could use it to fill my tree view, and do additional queries when the user drills down in the tree. Would be nice to have multiple levels in one query, though. – Marcel van Pinxteren Apr 10 '18 at 08:09
  • Whats your conception of multiple levels? – kavin Apr 10 '18 at 08:11
  • I expanded the sample result. A building can have multiple rooms, in each room there can be multiple parts, which each might consist of more parts. So, in theory, there is an unlimited number of levels of assemblies containing other assemblies. – Marcel van Pinxteren Apr 10 '18 at 08:56
  • database is schemaless... so you can have flexibility in storing data... you could have those multiple levels by combining subqueries with collector/count/aggregator statements – kavin Apr 10 '18 at 09:23
1

I know you want to avoid Foxx, but if interested I've answered this for someone else who wanted to have recursive nesting for menu trees.

Have a look at my answer for Arangodb AQL recursive graph traversal for more info.

David Thomas
  • 1,894
  • 2
  • 14
  • 18
  • 1
    The problem with this approach is, that you fire a query for every assembly. In a building with 50,000 parts, that will become a problem. I think I will try to process the output of the graph query into a tree in my client code. Maybe a good addition for the arangodb_ecto adapter we are working on. – Marcel van Pinxteren Apr 10 '18 at 19:45