Search code examples
cypher

match, iterate, create and create relationship assistance


Using this pseudo code, how can I do this entirely in cypher?

match-all nodes with the property of type: Images

using properties from the original match

match using the properties from the main match for type: Parent nodes

if no nodes of the parameters with the type: Parent create a new node using the parameters and type: Parent

match using the properties from the main match for type: Parent nodes

create a parent -> image relationship, using the node from the initial match and the match for type: Parent node


Solution

  • MATCH (img:Images)   //matches all nodes with the label Images
    WITH img             //carries forward the matched img nodes to the next clause.
    MERGE (parent:Parent {property1: img.property1, property2: img.property2, ...})  // finds or creates a Parent node with the properties taken from the matched img nodes. Replace property1, property2, etc., with the actual property names you want to use.
    MERGE (parent)-[:RELATIONSHIP_TYPE]->(img)  //finds or creates a relationship between the parent node and the img node. Replace RELATIONSHIP_TYPE with the actual relationship type you want to use.