alexanoid
alexanoid

Reputation: 25790

Neo4j Cypher update node dynamic properties

I have the following Neo4j nodes: Value. Each Value node can have 0..N properties in the following format:

Value1 node properties:
    property.1 = 123
    property.23 = 1
    property.452 = 5

Value2 node properties:
    property.45 = 90
    property.4 = 7

...

ValueN node properties:
    property.12 = 2
    property.46 = 17
    property.101 = 32
    property.3000 = 84

I don't know the certain number of these properties for every Value node but during the Cypher query, I need to update all of them.

I need to write Cypher query that will update all Value nodes in the database and increment every value of every Value.property.X +1.

Please note that the Value node can also contains other properties but I only need to update the properties in the mentioned format: property.X where X can be any number.

Please show how it can be done.

Upvotes: 0

Views: 1695

Answers (2)

B D T
B D T

Reputation: 83

Trying to dynamically set a property value in Neo4j (i.e., use a variable for the property name) results in an error like
Invalid input '[': expected ":"
I imagine this is the syntax error that @adnanmuttaleb mentioned, which is described better within the setProperty documentation (see the section on Usage Examples).

Below is the code from tomaž-bratanič's reply, modified to use apoc.create.setProperty instead.

MATCH (n:Node)
UNWIND keys(n) as key
WITH n, key WHERE key CONTAINS 'property.'
CALL apoc.create.setProperty(n, key, n[key]+1) YIELD node as N
RETURN properties(N)

Upvotes: 1

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6514

Try something like

MATCH (n:Node)
UNWIND keys(n) as key
WITH n,key where key contains 'property.'
SET n['key'] = n['key'] + 1

Upvotes: 3

Related Questions