25
votes

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

6
Best way of doing this query is: stackoverflow.com/a/42833702/277345Andrey Nikishaev
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

6 Answers

46
votes

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

MATCH (:User {username: 'user6'})-[r]-()
RETURN r
26
votes

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

10
votes

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
6
votes
MATCH (n1:Node1)-[:HAS_RELATIONSHIP]-(OtherNodes)
RETURN n1, OtherNodes

This will get Node1 and its relations with other nodes

enter image description here

1
votes

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
0
votes
  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;