简体   繁体   中英

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.

What is the proper syntax to return all the created nodes?

I got the answer from the 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. I highly recommend gravitating towards unwind as much as possible.

We see the best performance with it in all tested cases so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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