apoc.nodes.group

This procedure returns virtual nodes that can only be accessed by other APOC procedures. For more information, see Virtual Nodes & Relationships (Graph Projections).

This procedure is not considered safe to run from multiple threads. It is therefore not supported by the parallel runtime (introduced in Neo4j 5.13). For more information, see the Cypher Manual → Parallel runtime.

Details

Syntax

apoc.nodes.group(labels, groupByProperties [, aggregations, config ]) :: (nodes, relationships, node, relationship)

Description

Allows for the aggregation of NODE values based on the given properties. This procedure returns virtual NODE values.

Input arguments

Name

Type

Description

labels

LIST<STRING>

The list of node labels to aggregate over. Use ['*'] to indicate all node labels should be looked at.

groupByProperties

LIST<STRING>

The property keys to group the nodes by.

aggregations

LIST<MAP>

The first map specifies the node properties to aggregate with their corresponding aggregation functions, while the second map specifies the relationship properties for aggregation. The default is: [{*=count}, {*=count}].

config

MAP

{ includeRels :: STRING | LIST<STRING> excludeRels :: STRING | LIST<STRING>, orphans = true :: BOOLEAN, selfRels = true :: BOOLEAN, limitNodes = -1 :: INTEGER, limitRels = -1 :: INTEGER, relsPerNode = -1 :: INTEGER, filter :: MAP }. The default is: {}.

Return arguments

Name

Type

Description

nodes

LIST<NODE>

A list of grouped nodes represented as virtual nodes.

relationships

LIST<RELATIONSHIP>

A list of grouped relationships represented as virtual relationships.

node

NODE

The grouping node.

relationship

RELATIONSHIP

The grouping relationship.

Usage Examples

The examples in this section are based on the following sample graph:

CREATE
 (alice:Person {name:'Alice', gender:'female', age:32, kids:1}),
 (bob:Person   {name:'Bob',   gender:'male',   age:42, kids:3}),
 (eve:Person   {name:'Eve',   gender:'female', age:28, kids:2}),
 (graphs:Forum {name:'Graphs',    members:23}),
 (dbs:Forum    {name:'Databases', members:42}),
 (alice)-[:KNOWS {since:2017}]->(bob),
 (eve)-[:KNOWS   {since:2018}]->(bob),
 (alice)-[:MEMBER_OF]->(graphs),
 (alice)-[:MEMBER_OF]->(dbs),
 (bob)-[:MEMBER_OF]->(dbs),
 (eve)-[:MEMBER_OF]->(graphs);
CALL apoc.nodes.group(
  ['*'],
  ['gender'],
  [{`*`:'count', age:'min'}, {`*`:'count'} ]
)
YIELD relationships
UNWIND relationships as rel
RETURN apoc.rel.startNode(rel) AS start, rel, apoc.rel.endNode(rel) AS end;
Results
start rel end

(:Person {gender: "female", min_age: 28, `count_*`: 2})

[:MEMBER_OF {`count_*`: 3}]

(:Forum {gender: NULL, `count_*`: 2})

(:Person {gender: "female", min_age: 28, `count_*`: 2})

[:KNOWS {`count_*`: 2}]

(:Person {gender: "male", min_age: 42, `count_*`: 1})

(:Person {gender: "female", min_age: 28, `count_*`: 2})

[:KNOWS {`count_*`: 2}]

(:Person {gender: "male", min_age: 42, `count_*`: 1})

(:Person {gender: "male", min_age: 42, `count_*`: 1})

[:MEMBER_OF {`count_*`: 1}]

(:Forum {gender: NULL, `count_*`: 2})