Reputation: 2611
I tried updating the lables of a node Dynamically, but not succeeded. I want to remove all the labels of a node and then I wanted to set new list of labels to the same node.
My approach (failed, tried removing the labels only):
WITH ['a','b','c'] as newlabels
MATCH (n:people{name:'mam'})
WITH n,newlabels,labels(n) as oldlabels
FOREACH (l in oldlabels | REMOVE n:l)
SET n:newlabels
RETURN labels(n)
I suspect that, the labels cant be changed dynamically using the references. Is my understanding correct? And how can we achieve this?
Upvotes: 0
Views: 483
Reputation: 20185
No as you say dynamic labels modifications are not possible with raw cypher. Mainly because labels, property keys, relationship types are part of the query plan and used for caching.
There is the possibility to add labels dynamically with the APOC procedures, like explained here :
MATCH (n:Movie)
CALL apoc.create.addLabels( id(n), [ n.genre ] ) YIELD node
REMOVE node.studio
RETURN node
Maybe a feature to replace/remove existing labels could be a nice addition to the APOC.
UPDATE
After checking the source of APOC, there is a setLabels procedure which will remove non-matching passed labels, for example :
WITH ["A","B"] as labels
MATCH (m:Movie)
WITH collect(id(m))[0..20] as movies, labels
CALL apoc.create.setLabels(movies, labels) YIELD node RETURN node, labels(node)
│node │labels(node)│
╞══════════════════════════════╪════════════╡
│{tagline: Prepare to enter a s│[B, A] │
│ecret world of assassins, titl│ │
│e: Ninja Assassin, released: 2│ │
│009} │ │
├──────────────────────────────┼────────────┤
│{tagline: Walk a mile you'll n│[B, A] │
│ever forget., title: The Green│ │
│ Mile, released: 1999} │ │
├──────────────────────────────┼────────────┤
│{tagline: 400 million people w│[B, A] │
│ere waiting for the truth., ti│ │
│tle: Frost/Nixon, released: 20│ │
│08} │ │
├──────────────────────────────┼────────────┤
│{tagline: He didn't want law. │[B, A] │
│He wanted justice., title: Hof│ │
│fa, released: 1992} │ │
├──────────────────────────────┼────────────┤
Problem solved :)
Upvotes: 3