0
votes

I currently have a query that builds a relationship between Men nodes and People nodes:

MATCH (m:Mem)
UNWIND m.personID as person
MERGE (p:Person{personID:person})
MERGE (m)-[:WITH]->(p)

The Mem nodes contain an array of PersonIDs that I am unwinding and then matching to the Person nodes with the corresponding PersonIDs. However, the query is building the relationship with new Person nodes that it creates, with just the corresponding personIDs property (and no other properties) instead of building the relationship with the existing Person nodes with the corresponding personIDs.

This is happening even though I have a unique constraint on the personID property for nodes with the Person label.

How can I write a query that build the relationships but doesn't create new nodes with the corresponding personIDs?

1
Since you are using MERGE (instead of CREATE) for the Person node, you would only be creating a new Person node if there is no node with the same personID value. You need to check why the expected Person nodes do not already exist.cybersam
Ok - I think I've worked it out. When I unwind the Mems property array the personIDs are strings but on the existing Person nodes the personIDs are integers, so when it does the merge it's creating new nodes with the corresponding personIDs as strings instead of creating the relationships with the existing nodes that have the personIDs as integers. So I need to work out how to set the values from the array as integers when they're unwound instead of leaving them as strings.Sean

1 Answers

1
votes

Since the existing nodes store personID as an integer, you need to convert the person string values to integers via the TOINTEGER() function:

MATCH (m:Mem)
UNWIND m.personID as person
MERGE (p:Person {personID: TOINTEGER(person)})
MERGE (m)-[:WITH]->(p)