简体   繁体   English

在Neo4j Cypher Query中的FOREACH之后无法返回节点

[英]Unable to RETURN nodes after FOREACH in Neo4j Cypher Query

We are creating a time tree, and this query here works: 我们正在创建一个时间树,这个查询在这里工作:

CREATE (c:Century {century_label: 2000})
MATCH (c:Century {century_label: 2000})
WITH range(1900,1905) as YEARS, c
FOREACH (year in YEARS | CREATE (y:Year {year_label:year})-[r:YEAR_IN]->(c))

This one doesn't: 这个没有:

CREATE (c:Century {century_label: 2000})
MATCH (c:Century {century_label: 2000})
WITH range(1900,1905) as YEARS, c
FOREACH (year in YEARS | CREATE (y:Year {year_label:year})-[r:YEAR_IN]->(c))
RETURN y as YEAR

The problem is the y as YEAR , Neo4j throws an error about y , so it seems that you cannot do FOREACH and then collect the results after. 问题是y as YEAR ,Neo4j引发关于y的错误,所以看起来你不能做FOREACH然后收集结果。

What is the proper syntax to return all the created nodes? 返回所有已创建节点的正确语法是什么?

I got the answer from the Neo4j Slack: 我得到了Neo4j Slack的答案:

MATCH (c:Century {century_label:$century})
UNWIND range(1900,1999) as year
CREATE (y:Year {year_label:year})-[r:YEAR_IN]->(c)
RETURN y AS YEAR

Based on my reading as well, I believe this UNWIND is the successor to some of these more antiquated queries. 根据我的阅读,我相信这个UNWIND是其中一些更为陈旧的查询的继承者。 I highly recommend gravitating towards unwind as much as possible. 我强烈建议尽量放松。

We see the best performance with it in all tested cases so far. 到目前为止,我们在所有测试案例中都看到了它的最佳性能。

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

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