How to retrieve nodes for multiple depth relationships Neo4j Database Cypher?

丶灬走出姿态 提交于 2019-12-10 13:25:39

问题


Assuming that there is a simple graph as follows,

(City {name:gotham})<-[:LOCATED]-(Tower {name:abc})<-[:LOCATED]-(Bank:{name:CityBank})
(City {name:gotham})<-[:LOCATED]-(Cinema {name:MainHall})
(City {name:gotham})<-[:LOCATED]-(Bank {name:ComBank})

How can i get all banks located in City named Gotham in Neo4j Database (including CityBank and comBank)? I tried following pattern, it returned all the nodes LOCATED in City named gotham (Including Cinema as well)

MATCH (City{name:'Gotham'})<--(Bank) RETURN Bank

回答1:


Assuming you just mistyped the query, what isn't working?

MATCH (:City{name:'Gotham'})<--(bank:Bank) RETURN bank

should work fine.

Completely wrong as typed, should have read (the lonely star indicating, all relationships of any type, any length path):

MATCH (:City{name:'Gotham'})<-[*]-(bank:Bank) RETURN bank

Better would be:

MATCH (:City{name:'Gotham'})<-[:LOCATED*1..2]-(bank:Bank) RETURN bank

So as only to traverse LOCATED relationships. You can adjust the *1..2 (match paths of length 1 to 2) to meet path length requirements (if for example you added a Street or Block node, you might want to match paths up to length 3 *1..3)



来源:https://stackoverflow.com/questions/26798442/how-to-retrieve-nodes-for-multiple-depth-relationships-neo4j-database-cypher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!