cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher UPSERT

Its a MERGE question - sorry if its been asked before....

I'm looking for UPSERT functionality - I know the graph pattern I want to create but I don't know how much of the pattern already exists...

How do I match & return the part of the pattern that exists & then use that to build up the rest?

e.g. MERGE(server:Windows VMWare Server{name:'Server1',type:'Windows VMWare Server',scope:'UK'})-[:PARENT_OF]->(:Logical Server{name:'Operating System',type:'Logical Server',scope:'UK'})-[:PARENT_OF]->(:PagingFile{name:'pagefile.sys'})

BUT Server1 & Operating System already exist - I just need to add the pagefile.sys node to the existing nodes.

I guess I need to MATCH the pattern, work out what nodes already exist & pass down to a MERGE to add the missing nodes/relationships.

I hope this makes sense!

Mike

2 REPLIES 2

In this case you'd want to MERGE each of the nodes, then merge the relationships between them:

MERGE (server: `Windows VMWare Server` {name:'Server1',type:'Windows VMWare Server',scope:'UK'}) 
MERGE (logical: `Logical Server` {name:'Operating System',type:'Logical Server',scope:'UK'})
MERGE (paging: `PagingFile` {name:'pagefile.sys'})

MERGE (server)-[:PARENT_OF]->(logical)
MERGE (logical)-[:PARENT_OF]->(paging)

This is only if the nodes here should be unique, as opposed to unique on each path.

Thanks Andrew - This is real light bulb moment!

It didn't occur to me you could stack up commands like that - I have been round tripping each command one by one in my PowerShell\Bolt script...