25
votes

I have created a node with a wrong label.
Is there any way to change node label or relationship type without re-creating it? I have tried something like

MATCH n WHERE Id(n)=14 SET n.Labels = 'Person'

but it is fault...

4

4 Answers

38
votes
MATCH (n:OLD_LABEL {id:14})
REMOVE n:OLD_LABEL
SET n:NEW_LABEL

Guess this query explains itself.

31
votes

You can change the nodes associated with a label but you can't change the type of a relationship. Conceptually, if you take your chicken out of one coop and put it in another you haven't altered the substance of the chicken. But by the time you take the chicken out of the oven and put it in your mouth, it's not a chicken anymore (except equivocally). You can decide to call your cat Whiskers instead of Charlie, but if you decide you want an anaconda for a pet instead of a cat, it doesn't help to give the cat a new name. Similarly, a node can be a member of different labels and remain the same node, but a relationship's type is constitutive. So: you can add and remove labels as you please, but if you want a different relationship type then what you want is really a different relationship. This is also why a relationship has exactly one type, but a node can have many labels.

Labels are arbitrary sets or bags of nodes. The grammar for changing bags has already been given, but for completeness:

MATCH (n)
WHERE ID(n) = 14 
REMOVE n:Whiskers
SET n:Charlie

MATCH (petless_and_unhappy)-[whiskers:CAT]->(petful_and_unhappy)
DELETE whiskers
CREATE (petless_and_unhappy-[sir_hiss:ANACONDA]->(peftul_and_happy)
2
votes

This should work for changing labels on multiple nodes simultaneously:

MATCH (n:OLD_LABEL)
WHERE ID(n) IN [7, 8]
REMOVE n:OLD_LABEL
SET n:NEW_LABEL
0
votes

Try this:

Match (n:person{name:'John Jjones'}) Set n.name = 'John Jones'