2

I have this situation

CREATE (a:X { id: 1 })
CREATE (b:Y { id: 2 })
CREATE (c:Z { id: 3 })
CREATE (:Z { id: 4 })
CREATE (a)-[:HAS]->(d:W { id: 5 })<-[:HAS]-(b)
CREATE (c)-[:HAS]->(d)

Initial situation

And I want to run this query

MATCH (a:X { id: 1 })
MATCH (b:Y { id: 2 })
MATCH (c:Z { id: 4 }) // The one with no relationships yet
MERGE (a)-[:HAS]->(d:W)<-[:HAS]-(b), (d)<-[:HAS]-(c)
ON CREATE SET d.id = 6

The problem is that I'm not allowed to put more than one path in a single MERGE, how can I do this?

This is the result I want to achieve

Expected result

Notice that I can't just do this

MATCH (a:X { id: 1 })
MATCH (b:Y { id: 2 })
MATCH (c:Z { id: 4 })
MERGE (a)-[:HAS]->(d:W)<-[:HAS]-(b) // This path already exists, but it uses the other `Z`
ON CREATE SET d.id = 6
MERGE (d)<-[:HAS]-(c) // Separate merge

Because the result would be

What I don't want

1 Answer 1

2

In order to create three relationships connecting three existing nodes to a new node with label W — but only if those nodes are not already so connected — you can do it using CREATE:

MATCH (a:X { id: 1 }), (b:Y { id: 2 }), (c:Z { id: 4 })
  WHERE NOT EXISTS { (a)-[:HAS]->(d:W)<-[:HAS]-(b), (d)<-[:HAS]-(c) }
CREATE (a)-[:HAS]->(d:W)<-[:HAS]-(b), (d)<-[:HAS]-(c)

If the pattern in NOT EXISTS already exists, then MATCH produces no rows for CREATE to execute over i.e. nothing is created.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry if I wasn't clear: The id was just to recognize the node, the real "id" of my node are the 3 relationships
OK I think I get it now. Answer updated.
Is there a way to make it so that I can return the new/existing "W" node from the query?
The node d (and a and b and c) is available for projecting after CREATE, so you can just add the line RETURN d to return that node in the query results.
Wouldn't your query skip the ones that already exist?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.