I have a graph with three node types: NodeX, NodeY, and NodeZ
I have the this cypher query:
MATCH (x:NodeX)-[*]->(d)
WHERE x.Name = 'pqr'
RETURN x,d;
Here (d) may could be either NodeY or NodeZ. I'm looking to handle different nodetypes separately. Something like:
MATCH (x:NodeX)-[*]->(d)
WHERE x.Name = 'pqr'
WITH d
CASE WHEN typeof(d)=NodeY THEN {MATCH (y:NodeY)-[*]-(z:NodeZ)}
WHEN typeof(d)=NodeZ THEN {MATCH (z:NodeZ)-[*]-(y:NodeY)}
RETURN y,z
y
and z
correspond to d
. Is this possible to do so?
neo4j Share on : The Cypher Query to fetch all nodes of a neo4j graph database. In the neo4j graph database, we use Cypher queries to add and get data. To retrieve all nodes or data points in the neo4j graph database we can use the above Cypher query which will show you all nodes in the graph database.
If you want to get the labels of a specify node, then use labels(node) ; If you only want to get all node labels in neo4j, then use this function instead: call db. labels; , never ever use this query: MATCH n RETURN DISTINCT LABELS(n) . It will do a full table scan, which is very very slow..
Properties Properties are key-value pairs that are used for storing data on nodes and relationships. The value part of a property: Can hold different data types, such as number , string , or boolean .
When you want to return all nodes, relationships and paths found in a query, you can use the * symbol. This returns the two nodes, the relationship and the path used in the query.
Nodes have "labels", not "types" (the latter term only applies to relationships).
To get the labels of a node, you can use the LABELS()
function. So, to test if node n
has the label Foo
, you can do something like this in Cypher:
CASE WHEN 'Foo' IN LABELS(n) THEN ...
However, the CASE
clause cannot contain a MATCH
clause.
[EDITED]
In your specific case, something like this query (which assumes that, as you said, the only labels possible for d
are NodeY
and NodeZ
) may work:
MATCH (x:NodeX)-[*]->(d)
WHERE x.Name = 'pqr'
WITH d, CASE WHEN 'NodeY' IN LABELS(d) THEN 'NodeZ' ELSE 'NodeY' END AS otherLabel
MATCH (d)-[*]-(other)
WHERE otherLabel IN LABELS(other)
RETURN d, other;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With