2016-01-23 100 views

回答

2

是的,你可以。

下面是一個例子查詢創建包含一個索引和第8天Day節點:

WITH 5 AS day1, 12 AS day2 
FOREACH (i IN RANGE(0, day2-day1) | 
     CREATE (:Day { index: i, day: day1+i })); 

此查詢打印出所得到的節點:

MATCH (d:Day) 
RETURN d 
ORDER BY d.index; 

,這裏是一個例子的結果:

+--------------------------+ 
| d      | 
+--------------------------+ 
| Node[54]{day:5,index:0} | 
| Node[55]{day:6,index:1} | 
| Node[56]{day:7,index:2} | 
| Node[57]{day:8,index:3} | 
| Node[58]{day:9,index:4} | 
| Node[59]{day:10,index:5} | 
| Node[60]{day:11,index:6} | 
| Node[61]{day:12,index:7} | 
+--------------------------+ 
1

FOREACH在迭代期間不產生索引。如果你想索引你可以使用rangeUNWIND組合是這樣的:

WITH ["some", "array", "of", "things"] AS things 
UNWIND range(0,size(things)-2) AS i 
// Do something for each element in the array. In this case connect two Things 
MERGE (t1:Thing {name:things[i]})-[:RELATED_TO]->(t2:Thing {name:things[i+1]}) 

這個例子迭代計數器i了,你可以使用索引i數組中訪問該項目。