简体   繁体   English

Neo4j - 使用Cypher从节点开始并将Traverse图表移动到指定的深度并查找节点和关系

[英]Neo4j - Using Cypher start at a node and Traverse graph to a specified depth and find nodes and relationships

I have a simple graph in which I am trying to start at a particular node and traverse a depth of 2. From this traversal I am trying to extract the names of nodes and relationships. 我有一个简单的图形,其中我试图从特定节点开始并遍历2的深度。从这个遍历我试图提取节点和关系的名称。

This is my query, 这是我的查询,

    START n=node(5)
    MATCH p=(n)-[r:Relation*0..2]-(m)
    RETURN n.name,r.name,m.name;

I get this error: 我收到此错误:

    Type mismatch: expected Map, Node or Relationship but was Collection<Relationship> (line 3, column 15)

In the error description, it points a ^ symbol to r.name 在错误描述中,它将^符号指向r.name

Can Someone help me understand this issue. 有人可以帮助我理解这个问题。 My goal is to get the names of relationships along way.. 我的目标是获得关系的名称..

From what I have understood, the r is being returned as a collection. 根据我的理解,r将作为集合返回。 Is there a way to display the individual names within the collection? 有没有办法在集合中显示个人姓名?

Sham, 假,

As you noted, the problem is that 'r' is a collection of relationships that may have 0, 1, or 2 elements. 如您所述,问题是“r”是可能包含0,1或2个元素的关系集合。 You can use the reduce function to create a string of the relationship names and return that string. 您可以使用reduce函数创建关系名称的字符串并返回该字符串。

START n=node(5)
MATCH (n)-[r:Relation*0..2]-(m)
WITH n, m, reduce(s = '', rel IN r | s + rel.name + ',') as rels
RETURN n.name, m.name, rels;

Grace and peace, 恩典与和平,

Jim 吉姆

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

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