neo4jcyphergraph-databases

How to get all connected nodes in neo4j


enter image description here

I want to get list of all connected nodes starting from node 0 as shown in the diagram


Solution

  • Based on your comment:

    I want to get a list of all the connected nodes. For example in the above case when I search for connected nodes for 0, it should return nodes- 1,2,3

    This query will do what you want:

    MATCH ({id : 0})-[*]-(connected)
    RETURN connected
    

    The above query will return all nodes connected with a node with id=0 (I'm considering that the numbers inside the nodes are values of an id property) in any depth, both directions and considering any relationship type. Take a look in the section Relationships in depth of the docs.

    While this will work fine for small graphs note that this is a very expensive operation. It will go through the entire graph starting from the start point ({id : 0}) considering any relationship type. This is really not a good idea for production environments.