Combine statement to match multiple properties

I am trying to figure out how to effectively add a new node using a cypher request. I am trying to combine multiple data sources, so you need to look for possible matching data. I have 3 data points that may or may not exist. If any of the data points match, I want to reuse an existing node. If none of the data points matches, I want to create a new node.

Creating a node, if it does not exist, is an exact precedent for MERGE. MERGE does not allow the WHERE clause, otherwise it would be pretty simple. Since I coincide with the data point - OR data-point-b OR data-point-c, I cannot figure out how to use MERGE like this, and all the properties.

This is not true, but I have to express my purpose:

MERGE (n:TYPE)
WHERE n.propertyA = "A" OR n.propertyB = "B" OR n.propertyC = "C"
ON MATCH SET n.propertyA = "A", n.propertyB = "B", n.propertyC = "C"  
ON CREATE SET n.timestamp = <now>, n.propertyA = "A", n.propertyB = "B", n.propertyC = "C"
RETURN n;

I thought I could use a package or transaction. I would appreciate any understanding or guidance as I am still learning Cypher.

+4
source share
2 answers

Not sure if you can do this in one statement, I will follow up on this issue to find out if there is a better route. You can do this in two, where first it finds and updates the existing nodes, and the second finds and creates the missing ones:

OPTIONAL MATCH (existing:TYPE) WHERE existing.propertyA = 'A' OR existing.propertyB = 'B' OR existing.propertyC = 'C'
WITH existing
WHERE existing IS NOT NULL SET existing.propertyA = 'A', existing.propertyB = 'B', existing.propertyC = 'C'
RETURN existing;

OPTIONAL MATCH (existing:TYPE) WHERE existing.propertyA = 'ZZ' OR existing.propertyB = 'ZZ' OR existing.propertyC = 'ZZ'
WITH existing
WHERE existing IS NULL MERGE (newNode:TYPE {propertyA: 'ZZ', propertyB: 'ZZ', propertyC: 'ZZ'})
RETURN newNode

, - . A B ( C ), , A null, B C , node, reset A (, , , , ).

+1

, , :

,

neo4j documentation merge node, , node, , node .

WHERE , node.

, :

MERGE (n:TYPE)
WHERE n.propertyA = "A" OR n.propertyB = "B" OR n.propertyC = "C"
ON MATCH SET n.propertyA = "A", n.propertyB = "B", n.propertyC = "C"  
ON CREATE SET n.timestamp = timestamp(), n.propertyA = "A", n.propertyB = "B", n.propertyC = "C"
RETURN n;

, , . :

MERGE (n:TYPE {propertyA : "A"})
ON MATCH SET n.propertyA = "A", n.propertyB = "B", n.propertyC = "C"  
ON CREATE SET n.timestamp = timestamp(), n.propertyA = "A", n.propertyB = "B", n.propertyC = "C"
RETURN n;

, , . MERGE , node. OR, , node:

CREATE (a:Node) SET a.propertyA = "A" OR a.propertyB = "B"

, , , , , , .

-1

Source: https://habr.com/ru/post/1613289/


All Articles