2

I got some result like this in rethinkdb using group

[  
   {  
      "group":[  
         0,
         "A"
      ],
      "reduction":14
   },
   {  
      "group":[  
         "B",
         0
      ],
      "reduction":2
   }
]

I used like r.....group(..).count() (I cannot mentioned all query because there are many steps). But my result should like be displayed like this:

[  
       {  
          "group":[  
             "A"
          ],
          "reduction":14
       },
       {  
          "group":[  
             "B"
          ],
          "reduction":2
       }
    ]

Even Better just key value pair [{"A":14},{"B":2}]

4

1 回答 1

1

这不是世界上最干净的 ReQL,但它确实有效!

r.expr([  
   {  
      "group":[  
         0,
         "A"
      ],
      "reduction":14
   },
   {  
      "group":[  
         "B",
         0
      ],
      "reduction":2
   }
])
.map(function (row) {
  // Return a tuple with the key and the value
  return [row('group').filter(function (row) { return row.eq(0).not() }).nth(0), row('reduction') ]
})
.map(function (row) {
  // Convert the tuple into an array by passing an array of key-value pairs
  return r.expr([row]).coerceTo('object')
})

您可以将这两个地图功能结合起来,但我个人认为将它们分开会更好,以使它们更易于理解。

我使用过r.expr,因此您可以将其复制/粘贴到数据资源管理器中并查看它是否有效,但您可以maps在查询结束时复制/粘贴最后两个,结果将是相同的。

结果:

[
  {
    "A": 14
  } ,
  {
    "B": 2
  }
]
于 2015-04-19T04:13:35.883 回答