Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all relationships for a node with cypher

Tags:

neo4j

cypher

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

like image 600
jas Avatar asked Jul 17 '16 16:07

jas


People also ask

How do I return all nodes and relationships in Neo4j?

When you want to return all nodes, relationships and paths found in a query, you can use the * symbol. This returns the two nodes, the relationship and the path used in the query.

What is the syntax for getting all the nodes under specific label in Neo4j?

If you want to get the labels of a specify node, then use labels(node) ; If you only want to get all node labels in neo4j, then use this function instead: call db. labels; , never ever use this query: MATCH n RETURN DISTINCT LABELS(n) . It will do a full table scan, which is very very slow..

Which query is used to return the count of the relationship for a node?

Using count(*) to return the number of nodes. The function count(*) can be used to return the number of nodes; for example, the number of nodes connected to some node n . The labels and age property of the start node n and the number of nodes related to n are returned.

When you create a relationship in Cypher You must specify a direction True or false?

Syntax: Creating relationships When you create the relationship, it must have direction. You can query nodes for a relationship in either direction, but you must create the relationship with a direction.


2 Answers

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

MATCH (:User {username: 'user6'})-[r]-() RETURN r 
like image 119
Ant P Avatar answered Oct 10 '22 21:10

Ant P


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

like image 30
jas Avatar answered Oct 10 '22 20:10

jas