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.

MERGE and relationships

mburbidg1
Node Clone

The following query:

MERGE (resources:Entity {id: '#resources'})
MERGE (dc1:Component {id: 'DocumentChildOf'})
    ON MATCH SET dc1.value = '{v1}'
    ON CREATE SET dc1.value = '{v1}'
MERGE (dc1)-[:ATTACHED_TO]->(resources)
MERGE (artwork:Entity {id: '#artwork'})
MERGE (dc2:Component {id: 'DocumentChildOf'})
    ON MATCH SET dc2.value = '{v2}'
    ON CREATE SET dc2.value = '{v2}'
MERGE (dc2)-[:ATTACHED_TO]->(artwork)

Produces the following graph:

My intention was that both #artwork and #resources nodes have a DocumentChildOf node as is shown in the following graph.

Somehow the relationships are matching more broadly than I would think. How can I modify the query to get the desired results?

2 REPLIES 2

mburbidg1
Node Clone

I think I understand what is happening. The following line is matching the DocumentChildOf node created earlier and attaching it to the second entity also.

MERGE (dc2:Component {id: 'DocumentChildOf'})

I'm just not sure how to modify the query to get the desired results.

Your understanding is correct.

Your component merges don’t have any identifying properties you are searching on. It looks like you want to create them each time the query is executed. If so, change the ‘merge’ to ‘create’ and just use a ‘set’ for each of the component merges.