apoc.create.setLabels

Details

Syntax

apoc.create.setLabels(nodes, labels) :: (node)

Description

Sets the given labels to the given NODE values. Non-matching labels are removed from the nodes.

Input arguments

Name

Type

Description

nodes

ANY

The nodes to set labels on.

labels

LIST<STRING>

The labels to set on the given nodes.

Return arguments

Name

Type

Description

node

NODE

The updated node.

Set labels using Cypher

Labels can be referenced dynamically in Cypher without using APOC.

Cypher syntax for setting a label dynamically
SET n:$(label)

The dynamically calculated label must evaluate to a STRING or LIST<STRING>. For more information, see the Cypher Manual → Dynamically setting a label.

Usage Examples

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

CREATE (:Movie {title: 'A Few Good Men', genre: 'Drama'});

We can move the 'genre' property to a label and remove it as a property, as well as removing any other labels, by running the following query:

MATCH (n:Movie)
CALL apoc.create.setLabels( n, [ n.genre ] )
YIELD node
REMOVE node.genre
RETURN node;
Results
node

(:Drama {title: "A Few Good Men"})

The Cypher equivalent to this is:

MATCH (n:Movie)
REMOVE n:$(labels(n))
SET n:$(n.genre)
REMOVE n.genre
RETURN n;

If we want to only add new labels and not remove existing ones, see apoc.create.addLabels