Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SET label : pass label name as parameter

I have a query like this:

unwind {data} as row with row MERGE (p:Book{guid:row.bookGuid}) set p.name=row.name, p:Science

I want to pass the label 'Science' as a parameter as this label is not same for all the rows which I am passing in {data}.

I tried below query, but this is throwing syntax error.

with parameter like: { guid:1, name:"testName1",label1:"Histroy"}

unwind {data} as row with row MERGE (p:Book{guid:row.bookGuid}) set p.name=row.name, p:row.label1

Any workaround?

Thanks

like image 712
Krishna Shetty Avatar asked Sep 14 '25 10:09

Krishna Shetty


2 Answers

You can use APOC apoc.create.addLabels():

UNWIND {data} as row WITH row
MERGE (p:Book{guid:row.bookGuid})
SET p.name=row.name
CALL apoc.create.addLabels(id(p), [row.label1])

Another example: Using Cypher and APOC to move a property value to a label

like image 80
Игорь Щербаков Avatar answered Sep 17 '25 20:09

Игорь Щербаков


Yeh it's not supported yet. If you want to make it work you have to do a bit of a hack using FOREACH which you'll have to do for each type of label:

unwind {data} as row with row 
FOREACH(ignoreMe IN CASE WHEN row.label = "Science" THEN [1] ELSE [] END |
    MERGE (p:Book:Science{guid:row.bookGuid}) 
    set p.name=row.name
)

FOREACH(ignoreMe IN CASE WHEN row.label = "Math" THEN [1] ELSE [] END |
    MERGE (p:Book:Math{guid:row.bookGuid}) 
    set p.name=row.name
)

And so on...

like image 43
Mark Needham Avatar answered Sep 17 '25 20:09

Mark Needham