34

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

jas
  • 1,539
  • 5
  • 17
  • 30

6 Answers6

59

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

MATCH (:User {username: 'user6'})-[r]-()
RETURN r
Ant P
  • 24,820
  • 5
  • 68
  • 105
  • but how do you get all the nodes that are related deeper – Manuel Hernandez Feb 02 '17 at 07:55
  • 8
    `MATCH (:User {username: 'user6'})-[r*1..3]-() RETURN r` will find nodes that are related deeper 1 to 3 times. –  Mar 06 '17 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 https://stackoverflow.com/a/42206827/6515775 and https://stackoverflow.com/a/54557950/6515775 – mr.mams Nov 29 '21 at 14:39
35

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

Community
  • 1
  • 1
jas
  • 1,539
  • 5
  • 17
  • 30
  • 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 01 '17 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
Geoherna
  • 3,523
  • 1
  • 24
  • 39
7
MATCH (n1:Node1)-[:HAS_RELATIONSHIP]-(OtherNodes)
RETURN n1, OtherNodes

This will get Node1 and its relations with other nodes

enter image description here

Obiii
  • 698
  • 1
  • 6
  • 26
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: 'someone@somewhere.com'})-[r1]-(b)-[r2]-(c)
RETURN n1, r1, r2, b, c LIMIT 500
John Bonfardeci
  • 466
  • 3
  • 10
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;
gameFrenzy07
  • 357
  • 1
  • 4
  • 16