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.

Syntax for returning distinct nodes

Shukaku
Node Link

So here's my dilemma...

I want to be able to find distinct pairs of node with some value (a: nodeA {number: 1}), that are found in node (b)
by distinct pairs I mean:
1,2
3,4

and not:
1,2
2,1

I'm just looking for the proper syntax so I can implement it in my code.

1 ACCEPTED SOLUTION

For distinct node pairs, once you have the variables for both nodes, you can use the following to get rid of mirrored results:

...
WITH n1, n2
WHERE id(n1) < id(n2)
...

View solution in original post

5 REPLIES 5

Hello @Shukaku

This is a way of doing this, with APOC you can also sort list of nodes if you need more data from them:

WITH [[1,2], [2,1], [3,4], [4,3], [5,6]] AS list
UNWIND list AS items
WITH apoc.coll.sort(items) AS items_sort, collect(items)[0] AS items_list
RETURN items_list

Regards,
Cobra

What if I'm unable to call apoc.coll.sort? is there an alternative to that?

To be honest, I'm curious if you find something only in Cypher I had this problem a few days ago and I only found this solution with APOC.

For distinct node pairs, once you have the variables for both nodes, you can use the following to get rid of mirrored results:

...
WITH n1, n2
WHERE id(n1) < id(n2)
...

Thank you @andrew.bowman and @Cobra!
Andrew's answer is what I was looking for.