问题
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 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)
来源:https://stackoverflow.com/questions/62802772/cypher-query-creating-new-nodes-instead-of-building-relationship-with-existing-n