2012-12-16 62 views
3

使用Cypher 1.8,還有一些功能工作的集合,並返回一個元素:獲取集合的第n個元素在Cypher支架

HEAD(expression)

START a=node(2) 
RETURN a.array, head(a.array) 

LAST(expression)

START a=node(2) 
RETURN a.array, last(a.array) 

但是我找不到福以返回集合的第n個元素。我錯過了什麼?

回答

9

目前沒有好辦法做到這一點。請在https://github.com/neo4j/neo4j提交一個功能請求

我見過人做頭(尾(tail(尾)(coll))) ,尤其是如果你談論的是第17個要素或更糟。

例子: http://console.neo4j.org/r/bbo6o4

更新: 這裏是一種使用減少和範圍內做到這一點。但它使你可以給至少第n個參數,即使它仍然令我生厭:

start n=node(*) 
with collect(n) as allnodes 
return head(reduce(acc=allnodes, x in range(1,3): tail(acc))); 

http://console.neo4j.org/r/8erfup

更新2(2013年8月31日):
新集合語法現在合併到2.0中,理論上將成爲M05的一部分!因此,您可以執行以下操作:

start n=node(*) 
with collect(n) as allnodes 
return allnodes[3]; // or slices, like [1..3] 

我會在更新快照文檔時添加一個鏈接。

+0

@fynn我已經提交:

match(n:Node) with n order by n.node_id // returns {"node_id":1} // you can also use apoc.agg.last return apoc.agg.first(n); 

若要列出UNWIND列表中第一個工作使用此功能的請求。語法就像:'n(17,a.array)'。 https://github.com/neo4j/neo4j/pull/443 –

+1

僅供參考,剛剛合併到2.0分支中:https://github.com/neo4j/neo4j/pull/1124 –

0

當前,隨着APOC Procedures 3.3.0.2的發佈,您可以使用aggregation functions

這樣一來,你可以這樣想:

create (:Node {node_id : 1}), 
    (:Node {node_id : 2}), 
    (:Node {node_id : 3}); 

match(n:Node) 
with n order by n.node_id 
// returns {"node_id":2} 
return apoc.agg.nth(n, 1); 

或:

with ['fist', 'second', 'third'] as list 
unwind list as value 
// returns 'second' 
return apoc.agg.nth(value, 1);