Sami Fakhfakh
Sami Fakhfakh

Reputation: 125

Find duplicate nodes neo4j

I want to find all duplicate nodes per type in a neo4j database

Example : i have node1 with properties : name,adress,phone i wan't to match all nodes that are duplicated without specifying the properties names in the query

Upvotes: -1

Views: 1420

Answers (3)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

You can try this as well:

Match (n1:Person)
Match (n2:Person) Where id(n1) <> id(n2) and properties(n1)=properties(n2)
RETURN n1, n2

To ignore certain properties, try something like this:

WITH ['author', 'location', 'traceId'] AS ignoredProperties
Match (n1:Person)
Match (n2:Person) Where id(n1) <> id(n2) and ALL(x IN keys(properties(n1)) WHERE x IN ignoredProperties OR n1[x] = n2[x])
RETURN n1, n2

Upvotes: 2

Maybe just group by and count:

match (p:Person)
with p.name as name,
p.adress as address,
p.phone as phone, 
count(*) as cnt
where cnt > 1
match (p:Person{name: name,  address: address, phone:phone})
return p

Upvotes: 0

jose_bacoy
jose_bacoy

Reputation: 12684

Check first if all properties are the same then check if all values are the same.

//check that all keys in n1 and n2 are the same
Match (n1:Person)
Match (n2:Person) Where n1 < n2 and keys(n1)=keys(n2) 
//check that all properties are the same in n1 and n2 
WITH n1, n2, [ k1 in keys(n1) | n1[k1] ] as n1_props, [ k2 in keys(n2) | n2[k2] ] as n2_props
Where n1_props = n2_props
RETURN n1, n2

Upvotes: 1

Related Questions