NeoMan
NeoMan

Reputation: 45

Neo4j : dynamically create node properties in foreach or as such, where the properties key/value are not known?

Here's where I am stuck.

set t[x] = value[1]

The property value/key is known only at runtime.

Looking for dynamically setting property values post creation of node something like this:

match (a:User) 
with distinct(a.name) as property 
Match(b:Product) with property, b.Phone as value  
foreach(x in property  | 
         create(t:Test) set t[x] = value[1])

(note the set t[x] = value[1])

Or

match (a:User) with distinct(a.name) as property
Match(b:Product) with property, b.Phone as value  
foreach(x in property  | 
     call apoc.apoc.create.nodes('Test', [x,value[0]))

(Note the apoc.apoc.create.nodes('Test', [x,value[0]))

Intension is to User Property's value becomes a Property for Test node.

Upvotes: 1

Views: 791

Answers (1)

stdob--
stdob--

Reputation: 29172

You can try the apoc.map.setKey function to set the properties of the map:

WITH {} as props
WITH apoc.map.setKey(props, "name", "Jack") as props
WITH apoc.map.setKey(props, "surname", "Smith") as props
CREATE (N:TEST) SET N = props
RETURN N

Upvotes: 3

Related Questions