[英]In Neo4j how can I create relationships between one node an a collection of other nodes?
I have a graph containing some nodes with :SubSystem
labels.我有一个图表,其中包含一些带有:SubSystem
标签的节点。 I want to add a new node with a :Document
label and create relationships with each of the existing :SubSystem
nodes.我想添加一个带有:Document
label 的新节点,并与每个现有的:SubSystem
SubSystem 节点创建关系。
When I execute this statement:当我执行此语句时:
MATCH (s:SubSystem)
CREATE (d:Document {title:'New Document'})
CREATE (d)-[:DEPICTS]->(s);
I was surprised when Neo4j created a new :Document
node for each :SubSystem
.当 Neo4j 为每个:SubSystem
SubSystem 创建一个新的:Document
节点时,我感到很惊讶。 I have 12 sub-systems, so I ended up with 12 new documents each related to one sub-system.我有 12 个子系统,所以我最终得到了 12 个新文档,每个文档都与一个子系统相关。 I would have expected this behavior had I written:如果我写的话,我会预料到这种行为:
MATCH (s:SubSystem)
CREATE (:Document {title:'New Document'})-[:DEPICTS]->(s);
But I was expecting that separating the CREATE
clauses would create 1 document then create relationships between that document and each of the sub-systems.但我期望分离CREATE
子句会创建 1 个文档,然后在该文档和每个子系统之间创建关系。
Can someone explain why this doesn't work as I was expecting.有人可以解释为什么这不能像我预期的那样工作。
EDIT:编辑:
I found a solution.我找到了解决方案。 This statement does what I wanted, but I still think my original attempt should have worked.这个声明做了我想要的,但我仍然认为我最初的尝试应该奏效。
CREATE (d:Document {title:'New Document'})
WITH d MATCH (s:SubSystem) CREATE (d)-[:DEPICTS]->(s);
A MATCH
clause generates one or more result rows (or aborts the query if no results are found). MATCH
子句生成一个或多个结果行(如果未找到结果,则中止查询)。 A subsequent read/write clause would be executed once per row .随后的读/写子句将每行执行一次。 Rearranging the order of the clauses, as you did, is one way to work around that (when possible).正如您所做的那样,重新排列子句的顺序是解决该问题的一种方法(如果可能)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.