Neo4j一对一关系

时间:2019-01-29 21:20:04

标签: neo4j cypher

我有两个节点,“客户端”和“构建器”。我只想查找客户端只有1个构建器的关系,而该构建器只有一个客户端,所以是1-1关系。到目前为止,我的查询是

MERGE(b:Person{name:csv.name) 
MERGE(c:Person{name:csv.name})
WITH collect(distinct b) as builder, collect(distinct c) as client
UNWIND builder as builders
UNWIND client as clients
WITH builders, clients
WHERE builders = 1 and clients = 1
MATCH (builders:Person)-[bu:builder_for]->(clients:Person)
WITH builders,clients, count(distinct bu) as builds
WHERE builds=1

RETURN distinct builders, clients

这仅返回一对多关系,并且在我的客户列表中仍显示重复项。

The highlighted are the ones I would want to return

更新实施Cyber​​sam非常成功,非常感谢!

1 个答案:

答案 0 :(得分:0)

假设您要获取所有{em> all builder / client对,它们之间只有一个builder_for关系,此查询将使用{{3 }} COUNT为此:

MATCH (builder:Person)-[rel:builder_for]->(client:Person)
WITH builder, client, COUNT(rel) AS rel_count
WHERE rel_count = 1
RETURN builder, client;

[更新]

如果相反,您想要builder / client对中builder仅具有一个client,反之亦然,则此查询应该起作用:< / p>

MATCH (builder:Person)
WHERE SIZE((builder)-[:builder_for]->()) = 1
MATCH (builder)-[:builder_for]->(client:Person)
WHERE SIZE(()-[:builder_for]->(client)) = 1
RETURN builder, client;

此查询使用有效的关联度检查(在WHERE子句中)以确保builderclient节点分别仅具有单个传出或传入的{{ 1}}关系。