cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Duplicate Results (Same result multiple times)

match (a:User {name: "Some Name"}), (b)<-[:rel1]-(a), (c)<-[:rel2]-(a) return a.id as id, a.name as name, collect(apoc.map.fromValues(["key1", b.value1, "key2", b.value2])) as aray, collect (c.prop) as property

for the last 2 collect(), I get the same value multiples times.
For example:
[{key1: value1}, {key2: value2}, {key1: value1}, {key2: value2}, {key1: value1}, {key2: value2}]
[prop1, prop2,prop3, prop1, prop2,prop3, prop1, prop2,prop3]

1 ACCEPTED SOLUTION

Since you're matching on the patterns to b and c nodes at the same time, you're generating a cartesian product between those results, and that becomes apparent when you collect(). (try doing a RETURN * after your MATCH in place of your current RETURN to see how the results look at that point in time to better understand this).

Don't be too hasty on your matching. Do a MATCH on a to b, then collect() the values involving b (leaving a as your grouping key), then do a separate MATCH to c, then collect() your c values.

Alternately, use pattern comprehensions instead.

View solution in original post

2 REPLIES 2

Since you're matching on the patterns to b and c nodes at the same time, you're generating a cartesian product between those results, and that becomes apparent when you collect(). (try doing a RETURN * after your MATCH in place of your current RETURN to see how the results look at that point in time to better understand this).

Don't be too hasty on your matching. Do a MATCH on a to b, then collect() the values involving b (leaving a as your grouping key), then do a separate MATCH to c, then collect() your c values.

Alternately, use pattern comprehensions instead.

Thank you for your response.