2014-09-19 154 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