简体   繁体   中英

Neo4J - How to merge nodes with the same attribute value (name) but different node labels

I currently have duplicate name values across different node labels and I want to merge them. The issue is that every question I've found online assumes that BOTH the attribute name and the labels are the same. The code I've executed to query the instances I'm referring to is:

MATCH (a)-[r:FEATURED_IN]->(b) WHERE a.name = b.name AND id(a) <> id(b)

So that means that 'a' is featured in 'b', obviously 'a' and 'b' refer to the different node labels but the values are the same. How can I perform a merge to ensure that the 'b' node is deleted and only the 'a' node is returned? I know I could do this manually but there are so many instances of this that I would like to find a quick fix.

Thanks in advance.

You can collect all names in the nodes and combine them. Then UNWIND (it is like a For loop) and return distinct name.

MATCH (a)-[r:SYN_OF]->(b) WHERE a.name = b.name AND id(a) <> id(b)
WITH collect(distinct a.name) + collect(distinct b.name) as names
UNWIND names as name
RETURN distinct name

Result:

╒═══════════╕
│"name"     │
╞═══════════╡
│"Same Name"│
└───────────┘

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM