apoc.nodes.collapse

Prior to the release of APOC 2025.07, this procedure was restricted on on-premise instances. To use it on an older version, it must be unrestricted. For more details, see Installation → Load and unrestrict.
Details

Syntax

apoc.nodes.collapse(nodes [, config ]) :: (from, rel, to)

Description

Merges NODE values together in the given LIST<NODE>. The NODE values are then combined to become one NODE, with all labels of the previous NODE values attached to it, and all RELATIONSHIP values pointing to it.

Input arguments

Name

Type

Description

nodes

LIST<NODE>

The list of node values to merge.

config

MAP

{ mergeRels :: BOOLEAN, selfRef :: BOOLEAN, produceSelfRef = true :: BOOLEAN, preserveExistingSelfRels = true :: BOOLEAN, countMerge = true :: BOOLEAN, collapsedLabel :: BOOLEAN, singleElementAsArray = false :: BOOLEAN, avoidDuplicates = false :: BOOLEAN, relationshipSelectionStrategy = "incoming" :: ["incoming", "outgoing", "merge"] properties :: ["overwrite", "discard", "combine"] }. The default is: {}.

Return arguments

Name

Type

Description

from

NODE

The recently collapsed virtual node.

rel

RELATIONSHIP

A relationship connected to the collapsed node.

to

NODE

A node connected to the other end of the relationship.

Example

Given this dataset:

CREATE (:Person {name: 'Alice'})-[:KNOWS]->(:Person {name: 'Charlie'})
CREATE (:Person {name: 'Bob'})-[:KNOWS]->(:Person {name: 'Dave'})

The following query collapses Alice and Bob into a single virtual node, which inherits all their labels and relationships:

MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
CALL apoc.nodes.collapse([a, b], {properties: 'combine'})
YIELD from, rel, to
RETURN from.name AS fromNames, labels(from) AS fromLabels, type(rel) AS relType, to.name AS toName
Results
fromNames fromLabels relType toName

["Alice", "Bob"]

["Person"]

"KNOWS"

"Charlie"

["Alice", "Bob"]

["Person"]

"KNOWS"

"Dave"

One row is returned per relationship connected to the collapsed virtual node - one inherited from Alice and one from Bob. The collapsed node is virtual and is not written to the graph. With properties: 'combine', any conflicting property values across the merged nodes are combined into arrays on the virtual node; use 'overwrite' to keep the last value or 'discard' to keep the first.