sranga
sranga

Reputation: 123

Neo4j and Cypher: set labels from collection

I have bunch of labels as input. I want to iterate through and set it. The following code does give the error:

MATCH (n:Node)
WITH collect(n.lab) as labels
FOREACH (x IN labels | SET n:x);

When I execute, I get the following message:

WARNING: Variable n not defined (line 3, column 28 (offset: 73)) "FOREACH (x IN labels | SET n:x)"

Upvotes: 2

Views: 278

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 16365

First, you have forgot to pass n to the next context. That is:

WITH n, collect(n.lab) as labels 

Second, you cannot set labels this way, with pure Cypher. When you do SET n:x you are adding a label x in the node n. But you can do it using the APOC procedure apoc.create.addLabels, this way:

MATCH (n:Node)
WITH n, collect(n.lab) as labels
call apoc.create.addLabels(n, labels) yield node
return *

Upvotes: 1

Related Questions