How to add a label to a node in a query


(Paolodipietro58) #1

I have a generic node labeled 'Person'.

how can I add a new dynamic label to the node?

  1. I select a dynamic property value:
  2. I would like to add this value as a label to all the nodes with label:Persona

SET labels(n) += n.nome returns an error
SET n:n.nome returns an error on .

any idea?

(Andrew Bowman) #2

Normally with Cypher labels and relationship types must be hard-coded, as the query planner uses this information when planning the query (before any execution can even take place).

That said, we do have a workaround via the APOC Procedures library via apoc.create.addLabels().

Here's an example:

MATCH (n:Persona)
WITH n, n.nome as nome
CALL apoc.create.addLabels(n, [nome]) YIELD node
RETURN count(n) as count

(Michael Hunger) #3

Why would you add a name as a label, that doesn't make any sense?

(Paolodipietro58) #4

@michael.hunger you are completely right. You can search as deep as you like but it is really difficult to find some meaningfull reason for this.

But it is a simple example to show that I have to add a label unknown at the query write time, which can depend by the parameter content or the return value of another query. In this case, it starts to have some sense.