How to add a label to a node in a query

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?

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
1 Like

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

@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.

Here's one.

I built a generic tool to import any kind of data where the label is unknown at the time of importing but part of the data itself. If the data can be described in terms of Nodes with a Name, Label, Properties and Values or Relations between Nodes with Properties and Values, the CQL to import that data can be reused without the need to modify it to the current data model. It depends on being able to dynamically add a Label, though.

If there is a better way to import any data model, I'd love to hear about it, because I can see that dynamically adding Labels is not ideal.

You can use some of the apoc procedures to create dynamic labels and rel-types or something like apoc.graph.fromDocument to turn nested documents automatically into graphs.