简体   繁体   English

Neo4j密码返回具有特定格式的路径

[英]Neo4j cypher return Path with a specific format

I have a cypher query to return the shortest Path between two nodes. 我有一个密码查询,以返回两个节点之间的最短路径。

I am using Java JdbcTemplate. 我正在使用Java JdbcTemplate。

MATCH (nodeA),(nodeB), p = shortestPath((nodeA)-[*..15]-(nodeB)) "  //
                + "WHERE ID(nodeA) = {1} and ID(nodeB) = {2} RETURN nodes(p) as nodes "  //
                + ",relationships(p) as edges

My problem is that I have a Util method that do the mapping of the jdbctempate result. 我的问题是我有一个Util方法来映射jdbctempate结果。 It only works when my cypher returns nodes suchs as : 它仅在我的密码返回诸如以下的节点时才有效:

RETURN { id : id(node), labels : labels(node), data: node } as node

Is there a way to make my initial cypher return the result in that format? 有没有办法使我的初始密码以该格式返回结果?

NODES returns a list of nodes, so you can use UNWIND to unpack the list into a set of rows. NODES返回节点列表,因此您可以使用UNWIND将列表解压缩为一组行。 So in your example... 所以在你的例子中

MATCH (nodeA),(nodeB), p = shortestPath((nodeA)-[*..15]-(nodeB))
WHERE ID(nodeA) = {1} AND ID(nodeB) = {2}
UNWIND nodes(p) as n
RETURN { id : id(n), labels : labels(n), data: n} as node, relationships(p) as edges

You can use WITH + COLLECT to fold the nodes back into a list, and then perform the same operation on relationships if you need. 您可以使用WITH + COLLECT将节点折回到一个列表中,然后根据需要对关系执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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