## 15.2. Nodes collapse

This section describes a procedure that can be used to collapse a list of nodes into a virtual node.

`apoc.nodes.collapse([<node>], {config})` | returns the list of nodes merged into a VirtualNode

### 15.2.1. Config:

On `apoc.nodes.collapse` with config properties you can choose from 3 different behavior:

- "properties": "overwrite" : if there is the same property in more node, in the new one will have the last relationship’s/node’s property value
- "properties": "discard" : if there is the same property in more node, the new one will have the first relationship’s/node’s property value
- "properties": "combine" : if there is the same property in more node, the new one a value’s array with all relationship’s/node’s values

If properties parameter isn’t set relationships properties are `discard`.

- "mergeVirtualRels: true/false" : give the possibility to merge relationships with same type and direction. (DEFAULT `true`)
- "selfRel: true/false" : give the possibility to create the self relationship. (DEFAULT `false`)
- "countMerge: true/false" : give the possibility count all the Nodes/Relationships merged. (DEFAULT `true`)
- "collapsedLabel: true/false" : give the possibility to add the label `:Collapsed` to the virtualNode. (DEFAULT `false`)

### 15.2.2. Nodes collapse example

With this dataset we have:

If we want to collapse the people living in the city to a single node, we pass them to the procedure.

    MATCH (p:Person)-[:LIVES_IN]->(c:City)
    WITH c, collect(p) as subgraph
    CALL apoc.nodes.collapse(subgraph,{properties:'combine'}) yield from, rel, to
    return from, rel, to

And get this result:

With this dataset we have:

If we also want to collapse them onto the city itself, we add the city node first to the collection.

    MATCH (p:Person)-[:LIVES_IN]->(c:City)
    WITH c, c + collect(p) as subgraph
    CALL apoc.nodes.collapse(subgraph) yield from, rel, to
    return from, rel, to

And get this result: