Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Neo4j – How to merge/join parent -> children tree structure nodes

i have a tree structure (project)-[:IS_PARENT_OF]->(projectChild)

A project can have multiple children, what i would like to do is a query that creates a virtual node that contains all the children of a parent.

So the node would have data like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

parent: "project 1",
children:['cohort', 'protocol', etc...]

I tried this query

MATCH (p:Project)-[:IS_PARENT_OF]->(c)
CALL apoc.create.vNode(['Project'], {name: p.name, children: [c.name]}) YIELD node
RETURN node

But i only get one child:

{
  "identity": -32,
  "labels": [
    "Project"
  ],
  "properties": {
"name": "project 1",
"children": [
      "Experimental design"
    ]
  }
}

Does someone know how to get multiple children in my vNode ?

>Solution :

Since you seem to have tried a form of pattern comprehension, this is probably what you need:

MATCH (p:Project)
CALL apoc.create.vNode(
            ['Project'],      
            {
              name: p.name, 
              children: [(p)-[:IS_PARENT_OF]->(c) | c.name]
            }
     ) YIELD node
RETURN node

which would return an empty array [] in case the project does not have children.

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading