0

我是 RethinkDB 的新手,需要通过对“id”进行分组来返回“value”的 min 、 max 和 avg,这是我的 Json 示例:

{
  "mycontent": [
    {
      "id": "000000000011",
      "value": "300"
    },
    {
      "id": "000000000012",
      "value": "500"
    },
    {
      "id": "000000000011",
      "value": "700"
    },
    {
      "id": "000000000013",
      "value": "200"
    },
    {
      "id": "000000000011",
      "value": "950"
    },
    {
      "id": "000000000012",
      "value": "150"
    }
  ]
}

我发现使用 RethinkDB 逻辑来理解如何做到这一点存在一些问题。有什么帮助吗?

4

1 回答 1

0

您可以使用通过id()获取值。一个问题是您的数据是字符串,因此您需要转换为数字才能工作。mycontentgroupavg

像这样的东西:

r.expr({
  "mycontent": [
    {
      "id": "000000000011",
      "value": "300"
    },
    {
      "id": "000000000012",
      "value": "500"
    },
    {
      "id": "000000000011",
      "value": "700"
    },
    {
      "id": "000000000013",
      "value": "200"
    },
    {
      "id": "000000000011",
      "value": "950"
    },
    {
      "id": "000000000012",
      "value": "150"
    }
  ]
})('mycontent')
  .merge({value: r.row('value').coerceTo('number')})
  .group('id')
  .avg('value')
于 2016-05-12T23:56:33.763 回答