Lets say I have 2 node types: Node1
and Node2
.
They both have the same property {order:Integer}
Now I want to create the relationship that nodes with order:2
have an "AFTER"
relationship with nodes with order:1
BUT, I only want to create the relationships within the same node type.
That is, Node1{order:2}
should have an AFTER
relationship to Node1{order:1}
but no relationship to Node2{order:1}
When I use a wildcard, it matches and creates relationships across node types which I do not want.
MATCH (s:%{order:2}), (f:%{order:1})
CREATE (s)-[o:AFTER]->(f)
How can I achieve what I want without hard-coding each edge creation explicitly? This is what I want to avoid:
MATCH (s1:Node1{order:2}), (f1:Node1{order:1}), (s2:Node2{order:2}), (f2:Node2{order:1})
CREATE (s1)-[o:AFTER]->(f1), (s2)-[o:AFTER]->(f2)
Basically I'm trying to figure out if there's a way to have smart wildcard (I'm using SAMETYPE
as a placeholder), that matches same to same. Like so:
MATCH (s:%SAMETYPE{order:2}), (f:%SAMETYPE{order:1})
CREATE (s)-[o:AFTER]->(f)
If each node in your database, has only one label, then this will work for you:
MATCH (s{order:2}), (f{order:1})
WHERE labels(s)[0] = labels(f)[0]
CREATE (s)-[o:AFTER]->(f)
In this query, we fetch all the nodes and check whether their label is same or not, and create the relationship accordingly.