Search code examples
neo4jcypherorientdbarangodb

Optional node aggregation


In a graph db, how do I count the number of times user has contributed to the node "comment" directly or indirectly. In the diagram below, the answer is 2 (1 direct, 1 indirect)

Diagram


Solution

  • Given your example diagram, in Neo4j you could use the following Cypher query to capture both direct (User)-->(Comment) relationships and indirect (User)-->(Comment)-->(Comment) using the variable length path operator:

    MATCH (u:User)-[*]->(:Comment)
    RETURN u, COUNT(*) AS num
    

    Edit

    As Bruno points out this query will return 3 for your example as it considers all paths to comments grouped by user. Instead, you're probably interested in the number of distinct Comment nodes connected to each user:

    MATCH (u:User)-[*]->(c:Comment) RETURN u, COUNT(DISTINCT c) AS num