Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-26-2022 11:35 PM
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.
04-27-2022 02:19 PM
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
04-27-2022 08:54 PM
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.
All the sessions of the conference are now available online