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.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.