简体   繁体   中英

Match pairs of nodes with reciprocal relationships

I have a data model where nodes of the same label are connected by relationships of the same type:

在此处输入图像描述

There is no hierarchy or 'logic' which of the :Concept nodes is the start node of the reciprocal relationship.

My goal is to get all unique pairs of nodes. Wen you match on the :Concept nodes, you get two pairs, one starting at 123 the other starting at 234 :

MATCH (c1:Concept)-[:REL]->(c2:Concept)
RETURN [c1.concept_id, c2.concept_id]

Result:

[123, 234]
[234, 123]

Is there a way to get each pair only once? The order of the pairs does not matter.

You could do this:

MATCH (c1:Concept)-[:REL]-(c2:Concept)
WHERE id(c1) > id(c2)
RETURN [c1.concept_id, c2.concept_id]

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