Search code examples
neo4jmemgraphdb

Returning all nodes with and without relationships


In the neo4j browser

MATCH (n)
RETURN n

shows all nodes with all relationships. How does one do this in memgraph lab?

I can display the relevant nodes with relationships:

MATCH links = (Projectlist)-[:references]-(Reflist)
RETURN links

this does not shows the nodes without relationships, only the ones with relationships. What am I missing? Currently I prefer to use memgraph lab & dashboard with the style editor as this makes the visualisation easier to show to others to understand the concept.


Solution

  • If you want to return nodes that have or do not have relationships and the relationships that exist, the following query will do what you want :

    MATCH (n)
    OPTIONAL MATCH (n)-[r]->(m)
    RETURN n, r, m
    

    The neo4j browser will by default execute an additional query to find relationships between nodes that are returned to the visualisation.

    So for the following query

    MATCH (n) RETURN n
    

    The following query is immediately executed by the Neo4j browser after the first query returned results to the view :

    MATCH (a)-[r]->(b) WHERE id(a) IN $existingNodeIds AND id(b) IN $newNodeIds RETURN r;
    

    This Neo4j browser feature can be enabled/disabled in the browser settings.

    enter image description here

    I'm not sure if something similar exists in memgraph lab.