cancel
Showing results for 
Search instead for 
Did you mean: 

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

Node creation based on the condition

Hi Team,
In neo4j I have created 2 parent nodes as A and B. The value of A and B will be like this.
A={databaseA:"tracker",name:"SAM"} B={databaseB:"consumer",name:"Stark"}
Now Dynamically I am creating some child node with relationship with A and B. As a Happy path I have created with both combination A and B

<>
MATCH (a:Table1)
WHERE a.databaseA="tracker"
MATCH (b:Table2)
WHERE b.databaseB="consumer"
CREATE (d: Consumer{name:"abc", age:24})
MERGE (d)<-[:REL_WITH_A]-(a)
MERGE (d)<-[:REL_WITH_B]-(b)
RETURN d
</>
Now node will be create and relationship will be created from A and B.
My Goal is to create a single query If the above statement fails it need to check the with the condition a.databaseA="tracker" in case it is true need to create the child node with A relationship only
CREATE (d: Consumer{name:"abc", age:24}) MERGE (d)<-[:REL_WITH_A]-(a)
Same rule will be applicable for B.

2 REPLIES 2

koji
Ninja
Ninja

Hi @ajaykumaresh9

You mean to create a relation only when a and b exist respectively?
How about subquery?

CREATE (d: Consumer{name:"abc", age:24})
WITH d
CALL {
  WITH d
  MATCH (a:Table1)
  WHERE a.databaseA="tracker"
  MERGE (d)<-[:REL_WITH_A]-(a)
}
CALL {
  WITH d
  MATCH (b:Table2)
  WHERE b.databaseB="consumer"
  MERGE (d)<-[:REL_WITH_B]-(b)
}
RETURN d

Hi koji,
Thank you for your response. In the above query I could see the node have been created. But relationship is created with A and B. In my case it should be either A or B. In normal terms query should handle IF ELSE condition. If condition satisfy with A then it should create relationship with A. If not else part it will create relationship with B.