简体   繁体   English

Neo4j 1对1关系

[英]Neo4j 1 to 1 Relationship

I have two nodes, "client" and "builder". 我有两个节点,“客户端”和“构建器”。 I want to find only the relationships where the client only has 1 builder, and that builder only has that one client so a 1-1 relationship. 我只想查找客户端只有1个构建器的关系,而该构建器只有一个客户端,所以是1-1关系。 So far my query is 到目前为止,我的查询是

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

This only returns though a 1 to many relationship and is still showing duplicates in my client list. 这只会返回一对多关系,并且在我的客户列表中仍显示重复项。

The highlighted are the ones I would want to return 突出显示的是我想返回的

UPDATE cybersam implementation worked, thank you so much! UPDATE Cyber​​sam实施成功了,非常感谢!

Assuming you want to get all the builder / client pairs that only have a single builder_for relationship between them, this query uses the aggregating function COUNT to do that: 假设您要获取所有仅具有一个builder_for关系的builder / client对,此查询将使用聚合函数 COUNT来做到这一点:

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

[UPDATE] [UPDATE]

If, instead, you want builder / client pairs in which the builder has only that one client , and vice versa, then this query should work: 相反,如果您想要builder / client对,其中builder只有一个client ,反之亦然,则此查询应该起作用:

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

This query uses efficient relationship degree-ness checks (in the WHERE clauses) to ensure that the builder and client nodes only have, respectively, a single outgoing or incoming builder_for relationship. 该查询使用有效的关系度检查(在WHERE子句中),以确保builder节点和client节点分别仅具有单个传出或传入的builder_for关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM