Neo4j - using Cypher starts from a node and traverse plot to a given depth and finding nodes and links

I have a simple graph in which I am trying to start at a specific node and traverse to depth 2. From this traversal, I am trying to extract the names of the nodes and relations.

This is my request,

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

      

I am getting this error:

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

      

In the error description, it points the ^ character to r.name

Can someone help me understand this problem. My goal is to get the names of the relationships along the way.

From what I understood, r is returned as a collection. Is there a way to display individual names in a collection?

+3


source to share


1 answer


Shi,

As you noted, the problem is that 'r' is a collection of relations that can have 0, 1, or 2 elements. You can use the decrement function to create a string of relationship names and return that string.

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

+8


source







All Articles