0

I'm new on RethinkDB and need to return min , max and avg of "value" by grouping on "id", here's an example of my 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"
    }
  ]
}

I find some problem understanding how to do that using RethinkDB logic. Any help ?

BitFlow
  • 37
  • 6

1 Answers1

0

You can use () to get the value out of mycontent and group by id. One issue is your data is string, hence you need to convert to number for avg to works.

something like this:

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')
kureikain
  • 2,304
  • 2
  • 14
  • 9
  • Thank you kureikain , but i need to get the AVG, Max and Min of my datas, i tried something like .group('id').avg('value').max(value).min(value) but i think i didn't yet understood how it works. – BitFlow May 13 '16 at 10:11