Question about creating an intermediate node

When we use MATCH and MERGE to create an intermediate node, for example like below. It seems to me there is a state maintained between MATCH and MERGE statements so that the pre-existing relationship property between Actor and Movie in the case of the property, role, is able to replicated in the new relationships using the new intermediate nodes created. Esp. the 1st MERGE statement to create Role nodes, which is totally different from a single, random MERGE (x:Role {name: r.role}). Although only r.role is used in the statement, the corresponding actor and movie node info is kept in the memory. Don't know if someone can share some insights on this. A follow-up question is if it's true that the corresponding nodes info is always kept for any relationship, then we can use the corresponding nodes info without explicitly declaring them, right? In another word, using r.role not only brings us a bunch of strings, but the corresponding nodes info are hidden but attached to them, right? Thank you so much.

MATCH (a:Actor)-[r:ACTED_IN]->(m:Movie)
MERGE (x:Role {name: r.role})
MERGE (a)-[:PLAYED]->(x)
MERGE (x)-[:IN_MOVIE]->(m)
SET r.role = null

When you match or merge, you have the option to bind any of the entities to a variable, which is indicated by the string preceding the colon. This binding allows you to reference the entity in the present and subsequent cypher statements. Entities have labels/type, properties, and an internal identifier (elementId). You can use the variable to access any of these attributes of the entity, as well as set the entities properties.

These bounded values will remain in scope in your query until a “with” statement, which you use to indicate which variables to pass to the next phase of your query.

Noted: binding variables is option. You don’t always need to place a variable before the colon. Only when you need to reference the entity elsewhere in your query.

1 Like

Thank you glilienfield.

1 Like