get all relationships for a node with cypher
Asked Answered
P

7

37

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

Pokelogan answered 17/7, 2016 at 16:56 Comment(2)
Best way of doing this query is: https://mcmap.net/q/425747/-neo4j-get-all-relationships-between-a-set-of-nodesCitrus
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.Hamm
K
61

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

MATCH (:User {username: 'user6'})-[r]-()
RETURN r
Kalfas answered 17/7, 2016 at 18:0 Comment(3)
but how do you get all the nodes that are related deeperPhraseograph
MATCH (:User {username: 'user6'})-[r*1..3]-() RETURN r will find nodes that are related deeper 1 to 3 times.Barrio
Since this answer is no longer valid for 3.1 and above, it should be updated accordingly. For recent versions, please check answers https://mcmap.net/q/412969/-get-all-relationships-for-a-node-with-cypher and https://mcmap.net/q/412969/-get-all-relationships-for-a-node-with-cypherHefty
P
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

Pokelogan answered 13/2, 2017 at 14:47 Comment(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?Guiscard
P
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
Pice answered 6/2, 2019 at 16:13 Comment(0)
G
7
MATCH (n1:Node1)-[:HAS_RELATIONSHIP]-(OtherNodes)
RETURN n1, OtherNodes

This will get Node1 and its relations with other nodes

enter image description here

Gayelord answered 2/5, 2018 at 7:38 Comment(0)
S
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
Spay answered 8/10, 2020 at 15:13 Comment(0)
J
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;
Jacquelyn answered 22/6, 2018 at 7:20 Comment(1)
consider adding small explanationJann
C
0

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

Match (n)-[r]->(m) Return n,r,m
Chirr answered 15/10, 2023 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.