简体   繁体   中英

How can I create edges within a collection programmatically in ArangoDB?

Suppose I am creating a collection to store a family tree(only parent relations are important). I have a vertex collection called people and an edge collection called edges .

A document in people collection looks like:

{"name" : "xyz", "age": 12, "uniqueid": 10011, "parent_uniqueid": 9808}

What is the best way I can populate the edges collection by AQL using the uniqueid and parent_uniqueid attributes.

The query below assumes that for every node p in people ,

  1. p._id = p.uniqueid ,
  2. p.parent_uniqueid = (parent of p)._id (if p has a parent).
FOR p in people
FILTER HAS(p, 'parent_uniqueid')
LET edges = (
  FOR e in edges
  FILTER e._from == p.parent_uniqueid and e._to == p._id
  RETURN 1
)
FILTER LENGTH(edges) == 0
INSERT {_from: p.parent_uniqueid, _to: p._id} into edges

Check the options for the INSERT clause at https://www.arangodb.com/docs/stable/aql/operations-insert.html#setting-query-options to see if any of those may apply to your case.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM