2
votes

I have imported a list of documents (in a collection named "assemblies"). One of the attributes is "parent_id". Based on this, I want to construct the graph, that is implicitly described by this attribute.

"id","name","parent_id"
"30","Top level"
"30.1","30.1 Child 1","30"
"30.2","30.2 Child 2","30"

This is the query, that I expected to give me the info for creating the edge collection (named "contains", so it is from parent to child):

FOR assy IN assemblies
  LET parent = (
    FOR parent IN assemblies
      FILTER parent.id == assy.parent_id
      RETURN parent
  )
  RETURN {_from: parent._key, _to: assy._key}

What am I doing wrong? Could you give me the full query for inserting the edges?

1

1 Answers

4
votes

The problem is that the result of your subquery in parent is an array and not an document. But there is actually no need of a subquery. You can also performe a join, which should offer better performance and is easier to read. You also have to use the value of _id insteadt of _key for the fields _from and _to of your edges.

The following query does exactly what you want.

FOR assy IN assemblies
  FOR parent IN assemblies
    FILTER parent.id == assy.parent_id
    INSERT {_from: parent._id, _to: assy._id} IN contains
    RETURN NEW

Node: the RETURN NEW is optional. You can check with it whether the import was successful. With larger amount of data I would drop this.