首页 > 解决方案 > 如何知道节点 x 是否具有节点 z 的每个实例,其中它与另一个节点 y 有很多关系,而另一个节点 y 与另一个节点 z 有一个关系?

问题描述

所以我有一个节点:Customer,它有很多 node :Order,并且与node:Order有 1 个关系,这里:Shipper调用:SHIP_VIA的是层次结构:

(customer:Customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(shipper:Shipper)

现在只有 3 个托运人,订单指向他们,一个客户会有很多订单。所以我的问题是如何获得与所有托运人一起发货的所有客户?请注意,每个托运人shipperID内部都有一个属性。这是我的代码,但除了一个“存在的地方”之外它不起作用

match (customer:Customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(shipper:Shipper) 

WHERE exists((customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->(:Shipper 

{shipperID:1})) and exists((customer)-[:PURCHASED]->(order)-[:SHIP_VIA]->

(:Shipper {shipperID:2})) and exists((customer)-[:PURCHASED]->(order)-

[:SHIP_VIA]->(:Shipper {shipperID:3})) return  customer, order , shipper;

标签: neo4jcypher

解决方案


使用collect()all()函数尝试此解决方案:

// match all shippers
match (shipper:Shipper)
// create a list containing all shippers and pass to the next context
with collect(shipper) as shippers

// match all customers which has purchased an order with each shipper
match (customer:Customer)
where all (shipper in shippers where (customer)-[:PURCHASED]->(:Order)-[:SHIP_VIA]->(shipper))

// return customers
return customer

推荐阅读