cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Need To create a node base on condition

Hi Team,
In recent days I have worked with the neo4j and I am impressed. In the Happy flow was able to create a node with relationship
MATCH (a:Tracker), (b:Table) , (c:Table2) WHERE EXISTS (a.TrackerID) AND EXISTS(b.TrackerID) AND EXISTS(c.TrackerID) CREATE(d: Consumer{name:"abc", age:24}})<-[rel:REL_WITH_A]-(a) MERGE (d)<-[re:REL_WITH_B]-(b) MERGE (d)<-[r:REL_WITH_C]-(c) RETURN a
And my Goal to achieve the negative flow. If this condition fails need to check with other 2(a,b) nodes and need to create relationship.

If satisfy with a and b need to create relationship.
EXISTS (a.TrackerID) AND EXISTS(b.TrackerID) CREATE(d: Consumer{name:"abc", age:24}})<-[rel:REL_WITH_A]-(a) MERGE (d)<-[re:REL_WITH_B]-(b)
Based on condition need to create relationship for the new node

2 REPLIES 2

glilienfield
Ninja
Ninja

I think I understand your requirement. It looks to me that the only difference between the two scenarios is the absence of the c:Table2 node. If true, the following looks like it works. It will always create the relationships between the a:Tracker and b:Table nodes and the d:Consumer node when the Tracker and Table nodes are matched. I will then create the relationship between c:Table2 node and the d:Consumer node when the c:Table2 node is found.

MATCH (a:Tracker)
WHERE EXISTS (a.TrackerID)
MATCH (b:Table)
WHERE EXISTS(b.TrackerID)
CREATE (d: Consumer{name:"abc", age:24})
MERGE (d)<-[:REL_WITH_A]-(a) 
MERGE (d)<-[:REL_WITH_B]-(b) 
WITH d
CALL {
    WITH d
    MATCH (c:Table2)
    WHERE EXISTS(c.TrackerID)
    MERGE (d)<-[:REL_WITH_C]-(c) 
}
RETURN d

The other options is to look into using the APOC 'apoc.do.when' method.

ameyasoft
Graph Maven

I am afraid your code is going to create a very dense Consumer node as you are creating the relationship just on the existsence of 'TrackerID' property and not on the property value.