2014-09-19 155 views
0

考慮下面的結果在我的暗號查詢,那裏的人都集合:暗號聚集和收集

[ 
    { 
     "people": [ 
     { 
      "id": 24749, 
      "matches": 1 
     }, 
     { 
      "id": 26026, 
      "matches": 1 
     }, 
     { 
      "id": 26223, 
      "matches": 1 
     }, 
     { 
      "id": 25121, 
      "matches": 1 
     }, 
     { 
      "id": 24632, 
      "matches": 1 
     }, 
     { 
      "id": 25708, 
      "matches": 1 
     }, 
     { 
      "id": 25182, 
      "matches": 1 
     }, 
     { 
      "id": 24826, 
      "matches": 1 
     }, 
     { 
      "id": 26186, 
      "matches": 1 
     }, 
     { 
      "id": 27001, 
      "matches": 1 
     }, 
     { 
      "id": 24243, 
      "matches": 1 
     }, 
     { 
      "id": 27255, 
      "matches": 1 
     }, 
     { 
      "id": 27145, 
      "matches": 1 
     }, 
     { 
      "id": 24126, 
      "matches": 1 
     }, 
     { 
      "id": 27463, 
      "matches": 1 
     }, 
     { 
      "id": 24069, 
      "matches": 1 
     }, 
     { 
      "id": 25210, 
      "matches": 1 
     }, 
     { 
      "id": 24994, 
      "matches": 1 
     }, 
     { 
      "id": 27331, 
      "matches": 1 
     }, 
     { 
      "id": 25793, 
      "matches": 1 
     }, 
     { 
      "id": 27312, 
      "matches": 1 
     }, 
     { 
      "id": 26206, 
      "matches": 1 
     }, 
     { 
      "id": 24252, 
      "matches": 1 
     }, 
     { 
      "id": 24714, 
      "matches": 2 
     }, 
     { 
      "id": 24612, 
      "matches": 1 
     }, 
     { 
      "id": 26964, 
      "matches": 1 
     }, 
     { 
      "id": 27101, 
      "matches": 1 
     }, 
     { 
      "id": 26730, 
      "matches": 1 
     }, 
     { 
      "id": 27211, 
      "matches": 1 
     }, 
     { 
      "id": 24783, 
      "matches": 2 
     }, 
     { 
      "id": 25336, 
      "matches": 1 
     }, 
     { 
      "id": 24128, 
      "matches": 1 
     }, 
     { 
      "id": 26186, 
      "matches": 1 
     }, 
     { 
      "id": 25125, 
      "matches": 2 
     }, 
     { 
      "id": 24069, 
      "matches": 3 
     }, 
     { 
      "id": 24607, 
      "matches": 1 
     }, 
     { 
      "id": 27055, 
      "matches": 1 
     }, 
     { 
      "id": 25336, 
      "matches": 3 
     }, 
     { 
      "id": 24128, 
      "matches": 2 
     }, 
     { 
      "id": 26716, 
      "matches": 1 
     }, 
     { 
      "id": 27331, 
      "matches": 1 
     }, 
     { 
      "id": 24069, 
      "matches": 1 
     } 
     ] 
    } 
] 

我(與CYPHER)如何可以遍歷人們收集信息,發現相同的「ID」的那些,將「匹配」項目加在一起,然後添加一個名爲「重複」或類似的新項目。

實例結果我試圖讓:

[ 
    { 
    "people": [ 
     { 
     "id": 24069, 
     "matches": 5, // all the "matches" of the duplicate 24069's added together 
     "duplicates": 3 // how may times the id 24069 was found in the collection called people 
     }, 

     // etc... 
    ] 
    } 
] 
+0

什麼是您當前的密碼查詢? – 2014-09-22 11:14:08

回答

0

查詢:

匹配(P:人)返回不同的(p.id),總和(p.matches)

也許應該給你你需要什麼

0

這是有點複雜,因爲簡單的辦法是茶將你的收藏分開並重新組裝。這是很多步驟,但是Cypher要做到這一點,並對每一步的做法提出意見。

// initial matching (whatever you match on to get your collection) 
MATCH (n:People) WITH collect(n) as people 

// Tear people collection apart for possessing 
UNWIND people as p 

// Reduce on id 
WITH COLLECT(DISTINCT p.id) as id, p.matches as m 

// For each id, sum and count the matches 
WITH {id:id, matches:SUM(m), duplicates:COUNT(m)} as people 

// Recollect rows into collection 
RETURN COLLECT(people) as people