Nipun
Nipun

Reputation: 4319

Preventing merge to create nodes

Is there are way wherein I can prevent merge in cypher to not create node if its not present. I have a query like

FOREACH(p in {props} |
   MERGE (I:Interface {IfIPAddress:p.OrigIPAddress})
  MERGE (I2:Interface {IfIPAddress:p.TermIPAddress})
MERGE (I)-[r:link]->(I2)
SET r = p)

where the props is a map of arrays.

In the first and second merge I do not want I and I2 to be created. Is it possbile?

Upvotes: 0

Views: 69

Answers (2)

jjaderberg
jjaderberg

Reputation: 9952

You can't match in a foreach loop, probably because of the more basic restriction that reading and updating operations must be completed separately, see docs. There may be ways to make MERGE act like MATCH with chewing gum and duct tape, but it's probably easier to accept this limitation and run a flat query for each operation. Is there some other reason why you wouldn't run a query like this?

MATCH (I:Interface {IfIPAddress:p.OrigIPAddress}), (I2:Interface {IfIPAddress:p.TermIPAddress})
MERGE (I)-[r:link]->(I2)
ON CREATE SET r = p

Run it on the transactional cypher endpoint, for instance with as many statements per transaction as there are iterations in your foreach loop.

Upvotes: 0

BtySgtMajor
BtySgtMajor

Reputation: 1462

Is it out of the question to iterate through the map and dynamically create a query for each map entry? I.e. don't do it in a single query but multiple?

Upvotes: 1

Related Questions