apoc.nodes.link

Details

Syntax

apoc.nodes.link(nodes, type [, config ])

Description

Creates a linked list of the given NODE values connected by the given RELATIONSHIP type.

Input arguments

Name

Type

Description

nodes

LIST<NODE>

The list of nodes to be linked.

type

STRING

The relationship type name to link the nodes with.

config

MAP

{ avoidDuplicates = false :: BOOLEAN } The default is: {}.

Config parameters

This procedure supports the following config parameters:

Config parameters
Name Type Default Description

avoidDuplicates

BOOLEAN

false

If true, the relationship will not be created, if it already exists

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

CREATE (:Event {name: "Event 1", date: datetime("2019-06-01")})
CREATE (:Event {name: "Event 2", date: datetime("2019-06-04")})
CREATE (:Event {name: "Event 3", date: datetime("2019-06-08")});

We can create a linked list of these events, by running the following query:

MATCH (e:Event)
WITH e ORDER BY e.date
WITH collect(e) AS events
CALL apoc.nodes.link(events, "NEXT")
RETURN count(*);
linked list events

We can check for relationship existence using the {avoidDuplicates: true} configuration; calling the previous query twice, 2 relations of type "NEXT" will be created between the nodes, instead, by executing CALL apoc.nodes.link(events, "NEXT", {avoidDuplicates: true}) only one relationship of type "NEXT" will be created.