简体   繁体   English

如何在neo4j密码中仅与一个节点(名称相似)建立关系?

[英]How to create relationship to just one node (with a similar name) in neo4j cypher?

I want to design a Database about airports. 我想设计一个关于机场的数据库。 Every airport has at least five terminals (named A, B, C, D and E). 每个机场至少有五个航站楼(分别为A,B,C,D和E)。 So I want to have Airport Schiphol INCLUDES A, B, C, D,E. 所以我想让史基浦机场包括A,B,C,D,E。 But I have created five terminals with the name "A" and thus if I run this code: 但是我创建了五个名为“ A”的终端,因此如果运行以下代码:

MATCH (a:Airport{name:"Schiphol"}),(b:Terminal{name:"A"}) CREATE (a)-[r: INCLUDES]->(b)

it attaches every Terminal A to Schiphol. 它会将每个候机楼A连接到史基浦。 How can I avoid this, and assign just 1 set of A , B, C, D and E to the airport? 我该如何避免这种情况,而只给机场分配一组A,B,C,D和E?

Thanks in advance 提前致谢

MERGE is going to be the solution here, creating the pattern of the airport including a terminal, but not creating the :Terminal nodes ahead of time. MERGE将成为此处的解决方案,创建包括航站楼的机场模式,但不提前创建:Terminal节点。

When you use MERGE for a pattern and one of the nodes is already bound (the :Airport node) and other isn't (the :Terminal), then if the pattern needs to be created the unbound node will be created along with the relationship between the nodes. 当您将MERGE用于模式时,其中一个节点已被绑定(:Airport节点)而另一个未绑定(:Terminal),则如果需要创建模式,则将创建未绑定的节点以及关系节点之间。 This is how you create terminals per airport. 这是您在每个机场创建航站楼的方式。

MATCH (a:Airport{name:"Schiphol"})
MERGE (a)-[:INCLUDES]->(termA:Terminal{name:"A"}) 

And if you want to create all the terminals at once per airport: 如果要在每个机场一次创建所有航站楼,请执行以下操作:

MATCH (a:Airport{name:"Schiphol"})
UNWIND ['A','B','C','D','E'] as term
MERGE (a)-[:INCLUDES]->(terminal:Terminal{name:term})

Why having five terminals with 'A' name ? 为什么要有五个带有“ A”名称的端子? A solution can be : 解决方案可以是:

MATCH (a:Airport{name:"Schiphol"})
MATCH (b:Terminal{name:"A"}) 
WITH a, COLLECT (b) as terminals
WITH a, HEAD(terminals) as terminal 
CREATE (a)-[r: INCLUDES]->(terminal)

(not tried) But it is arbitrary... (未尝试)但这是任意的...

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

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