Reputation: 729
So I need to make a relationship between nodes in the form (A) - managed_by -> (b)
I match them with a kind of id attribute, but the problem is that there are multiple b values with the same id attribute, and I only want to create the relationship to one. The important part to note is that the b nodes already have relationships that explain why these values are repeated. Lets say the relationship looks like this, with id numbers representing a b node:
1111 -has sub-> 1112 -has sub-> 1112 -has sub-> 1112 -has sub-> 1113 etc.
I want to create a managed_by relationship between an a node (with a value of 1112) and the top level b node that has 1112. So in this case, it would be the only 1112 b node that is not on the recieving end of a "has sub" relationship with a b node that has the same id.
My current query looks something like this:
MATCH(a:`a thing`),(b:`b thing`) WHERE a.resp_id = b.item_id
MERGE (a)-[r:`Managed By`]->(b)
In this case if a.resp_id is 1112, it creates three relationships, but I only want it to create a relationship to the top level b node with that value. How do I change this query to make that happen?
Upvotes: 0
Views: 226
Reputation: 30397
If this only applies to b nodes, then add a condition that the b node you match to must not have a b node above it with the same id:
MATCH(a:`a thing`)
WITH a, a.resp_id as resp_id
MATCH (b:`b thing`)
WHERE resp_id = b.item_id
AND NOT (:`b thing`{item_id:resp_id})-[:`has sub`]->(b)
MERGE (a)-[r:`Managed By`]->(b)
Upvotes: 1