2

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked. Turned out the field "id" had two invisible characters in front of it, due to encoding issues. After converting the csv file to basic UTF-8, it worked. I found this out, when I was exporting the collection to json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.