简体   繁体   中英

Use variable to match a node label in Neo4J CYPHER

I will be running some complex MATCH which will return a node label to me. Then I want to trim the prefix of that node label and use the rest of it to further perform matches. I am trying to do this by assigning that rest of the part of the returned label to the variable and then using that variable to the label the node in MATCH .

However I feel this does not work since we cannot use a variable to label a node in MATCH .

For example create simple two node graph:

CREATE (:PrefixNodeLabel)
CREATE (:NodeLabel)

This:

MATCH(n:PrefixNodeLabel)
RETURN substring(labels(n)[0],6)

correctly returns NodeLabel .

I am trying to use this to label a node:

MATCH(n:PrefixNodeLabel)
WITH substring(labels(n)[0],6) AS nodeLabel
MATCH(m:nodeLabel)   //using a variable to label a node
RETURN m

But this does not return the (:NodeLabel) node.

I know what I am doing may look weird. But anyway how do I do this?

you cannot pass variable in place of Label

You can achieve this by

MATCH (n:PrefixNodeLabel)
WITH SUBSTRING(labels(n)[0],2) AS nodeLabel
MATCH (m)   
WHERE nodeLabel IN LABELS(m)
RETURN m

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM