apoc.nodes.collapseProcedure
| 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. |
Syntax |
|
||
Description |
Merges |
||
Input arguments |
Name |
Type |
Description |
|
|
The list of node values to merge. |
|
|
|
|
|
Return arguments |
Name |
Type |
Description |
|
|
The recently collapsed virtual node. |
|
|
|
A relationship connected to the collapsed 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
| 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.