35

I would like to find out all the incoming and outgoing relationships for a node. I tried couple of queries suggested in other questions but not having much luck. These are the two I tried

MATCH (a:User {username: "user6"})-[r*]-(b)
RETURN a, r, b

I only have 500 nodes and it runs forever. I gave up after an hour.

I tried this

MATCH (c:User {username : 'user6'})-[r:*0..1]-(d)
WITH c, collect(r) as rs
RETURN c, rs

But I get this error

WARNING: Invalid input '*': expected whitespace or a rel type name (line 1, column 35 (offset: 34))
"MATCH (c {username : 'user6'})-[r:*0..1]-(d)"

What would be correct way to get all the relationships for a node?

I'm using version 3.0.3

2
  • Best way of doing this query is: stackoverflow.com/a/42833702/277345 Mar 16, 2017 at 12:11
  • Remove the colon in your second query. It should be MATCH (c:User {username : 'user6'})-[r*0..1]-(d). The colon is for labels only.
    – mcv
    Apr 25, 2019 at 14:36

7 Answers 7

61

The simplest way to get all relationships for a single node is like this:

MATCH (:User {username: 'user6'})-[r]-()
RETURN r
3
  • 1
    but how do you get all the nodes that are related deeper Feb 2, 2017 at 7:55
  • 9
    MATCH (:User {username: 'user6'})-[r*1..3]-() RETURN r will find nodes that are related deeper 1 to 3 times.
    – user6560716
    Mar 6, 2017 at 21:48
  • 2
    Since this answer is no longer valid for 3.1 and above, it should be updated accordingly. For recent versions, please check answers stackoverflow.com/a/42206827/6515775 and stackoverflow.com/a/54557950/6515775
    – mr.mams
    Nov 29, 2021 at 14:39
37

The above solution doesn't return a graph representation in 3.1 anymore. Instead below solution should work

MATCH (a:User {username: 'user6'})-[r]-(b)
RETURN r, a, b

This was answered in another SO question

1
  • 1
    yea, apparently the [r*1..3] feature was deprecated. Know off the top of your head how to query more than 1 degree of depth now?
    – geominded
    Sep 1, 2017 at 19:02
13

Most of these answers will work just fine, but if like me, you also need the name of the relationship itself, you need to wrap r with type():

MATCH (a:User {username: 'user6'})-[r]-(b)
RETURN type(r), a, b
7
MATCH (n1:Node1)-[:HAS_RELATIONSHIP]-(OtherNodes)
RETURN n1, OtherNodes

This will get Node1 and its relations with other nodes

enter image description here

4

This query will show all relationships and connected nodes for a single Person node filtered by their email, as a graph.

MATCH(n1:Person {email: '[email protected]'})-[r1]-(b)-[r2]-(c)
RETURN n1, r1, r2, b, c LIMIT 500
3
  1. Fetch all nodes:
START n=node () RETURN n 

OR

MATCH (n) RETURN n
  1. Displays the nodes and the relationships:
MATCH (n) MATCH (n)-[r]-() RETURN n,r

OR

START n=node() MATCH (n)-[r]->(m) RETURN n,r,m 
  1. Match nodes and relationships:
MATCH (a:Policy)-[:APPLIES_TO]-(Cluster) WHERE a.name = "pol-1nils" RETURN a, Cluster
  1. Get all object of particular nodes:
MATCH (list:Policy) RETURN list
  1. Bound to the entities between two nodes:
MATCH (a:WorkLoad)-[b:APPLIES_TO]->(c:Policy) WHERE c.name = "shamshad" RETURN a,b,c;
1
  • 1
    consider adding small explanation
    – Derrick
    Jun 22, 2018 at 7:39
0

For 5.3 Neo4j, this is working. To get the nodes and relationship

Match (n)-[r]->(m) Return n,r,m

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.