Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j - how to set label with property value

Tags:

neo4j

I have nodes without label but a property NodeType

Is there a way to set the label of those nodes with the value of the NodeType property?

Thanks!

like image 929
Olivier Ziller Avatar asked Oct 23 '14 20:10

Olivier Ziller


People also ask

How do I create a node label in Neo4j?

Create a Node with Multiple Labels To create multiple labels with a single node, you have to specify the labels for the node by separating them with a colon " : ". Syntax: CREATE (node:label1:label2:. . . . labeln)

What is property key in Neo4j?

propkeys — Property keys Property keys in the Neo4j database, returned as a cell array of character vectors. Each character vector denotes a property key.

What is label name in Neo4j?

Label is a name or identifier to a Node or a Relationship in Neo4j Database. We can say this Label name to a Relationship as "Relationship Type". We can use CQL CREATE command to create a single label to a Node or a Relationship and multiple labels to a Node.

What is APOC in Neo4j?

APOC is an add-on library for Neo4j that provides hundreds of procedures and functions adding a lot of useful functionality. The library covers a lot of things, that's why we provide a searchable Overview of all APOC functions and procedures.


2 Answers

No, currently there is no possibility to define a label with a variable.

You'll have to do it in your application by fetching all nodes that you want to add a label on it and sending a Cypher Query to add this label.

A quick example in PHP :

$nodes = $client->sendCypherQuery('MATCH (n) WHERE n.nodeType = "MyType" RETURN n');
foreach ($nodes as $node) {
    $label = $node->getProperty('nodeType');
    $id = $node->getId();
    $client->sendCypherQuery('MATCH (n) WHERE id(n) = '.$id.' SET n :'.$label;
}
like image 64
Christophe Willemsen Avatar answered Sep 28 '22 08:09

Christophe Willemsen


You can't use a variable but you can still do it in a cypher query (or at least a few of them) rather than a script. If you only have a handful of different labels this probably works well but not that scalable for many labels.

MATCH (n) 
WHERE length(labels(n)) = 0 
AND n.type = 'XX' 
SET n:XX;


MATCH (n) 
WHERE length(labels(n)) = 0 
AND n.type = 'XY' 
SET n:XY;
like image 29
Dave Bennett Avatar answered Sep 28 '22 09:09

Dave Bennett