我有两种类型的标记为A和B的节点,并通过关系R
相关联我尝试获取与具有相同属性值的B节点完全相关的A节点组
所以,下面的例子应该返回两组:(a2,a5)和(a3,a4)
(A)-->(B) | B.Property
------------------------
a1-->b1 | p1
a1-->b2 | p2
a1-->b3 | p3
a2-->b4 | p4
a3-->b5 | p2
a3-->b6 | p4
a4-->b7 | p2
a4-->b8 | p4
a5-->b9 | p4
a6-->b10 | p1
a7-->b11 | p3
a7-->b12 | p5
答案 0 :(得分:0)
我使用以下命令重现了您的示例数据集:
CREATE
(a1:A {name:'a1'}),
(a2:A {name:'a2'}),
(a3:A {name:'a3'}),
(a4:A {name:'a4'}),
(a5:A {name:'a5'}),
(a6:A {name:'a6'}),
(a7:A {name:'a7'}),
(a1)-[:R]->(b1:B {name: 'b1', property: 'p1'}),
(a1)-[:R]->(b2:B {name: 'b2', property: 'p2'}),
(a1)-[:R]->(b3:B {name: 'b3', property: 'p3'}),
(a2)-[:R]->(b4:B {name: 'b4', property: 'p4'}),
(a3)-[:R]->(b5:B {name: 'b5', property: 'p2'}),
(a3)-[:R]->(b6:B {name: 'b6', property: 'p4'}),
(a4)-[:R]->(b7:B {name: 'b7', property: 'p2'}),
(a4)-[:R]->(b8:B {name: 'b8', property: 'p4'}),
(a5)-[:R]->(b9:B {name: 'b9', property: 'p4'}),
(a6)-[:R]->(b10:B {name: 'b10', property: 'p1'}),
(a7)-[:R]->(b11:B {name: 'b11', property: 'p3'}),
(a7)-[:R]->(b12:B {name: 'b12', property: 'p5'})
此查询返回所需的对:
MATCH (a1:A)-[:R]->(b1:B) // 1
WITH *
ORDER BY ID(b1)
WITH a1, collect(b1.property) AS b1Properties // 2
MATCH (a2:A)-[:R]->(b2:B) // 3
WHERE ID(a1) < ID(a2) // 4
WITH *
ORDER BY ID(b2)
WITH a1, b1Properties, a2, collect(b2.property) AS b2Properties // 5
WHERE b1Properties = b2Properties // 6
RETURN a1, a2, b1Properties, b2Properties // 7
说明:
a1:A
- b1:B
对,按b1
个节点的标识符排序(以确保b1
始终处于相同的顺序)。property
个b1
个节点的值收集到列表b1Properties
。a2:A
- b2:B
对,ID(a1) < ID(a2)
进行过滤
a6, a6
),a6, a7
和a7, a6
)。property
节点的b2
值已排序并收集到b2Properties
列表。b1Properties
和b2Properties
相等的结果。 WITH *
行需要使用ORDER BY
子句,该子句必须以WITH
或RETURN
子句开头。
结果是:
╒══════════╤══════════╤════════════╤════════════╕
│a1 │a2 │b1Properties│b2Properties│
╞══════════╪══════════╪════════════╪════════════╡
│{name: a3}│{name: a4}│[p2, p4] │[p2, p4] │
├──────────┼──────────┼────────────┼────────────┤
│{name: a2}│{name: a5}│[p4] │[p4] │
└──────────┴──────────┴────────────┴────────────┘