Q:

RethinkDB: How to get data from dictionary field and transform them to lists

I have objects that have a following structure:
[
  {
    'record': 1,
    'tags': [1, 2],
    'data': {
      '1': 10,
      '2': 15
    }
  },
  ...
  {
    'record': 1,
    'tags': [3, 4, 5],
    'data': {
      '1': 100,
      '2': 150
    }
  }
]

How can I get distinct lists of data: [10, ..., 100] and [15, ..., 150] to process each of them? 
Thanks

A:

If all data have same key, I proposed this map-reduce process.  
.map(function(doc) {
  return doc('data').keys().map(function(key) {
     return [key, [doc('data')(key)]]
    }).coerceTo('object')  
})

.reduce(function(left, right) {
  return left.keys().map(function(key) {
      return [key, left(key).setUnion(right(key))]
    })
  .coerceTo('object')  
})   

With your data set, we have:
r.expr(
[
  {
    'record': 1,
    'tags': [1, 2],
    'data': {
      '1': 10,
      '2': 15
    }
  },

  {
    'record': 1,
    'tags': [1, 2],
    'data': {
      '1': 19,
      '2': 100
    }
  },

  {
    'record': 1,
    'tags': [3, 4, 5],
    'data': {
      '1': 100,
      '2': 150
    }
  }
]
)

.map(function(doc) {
  return doc('data').keys().map(function(key) {
     return [key, [doc('data')(key)]]
    }).coerceTo('object')  
})

.reduce(function(left, right) {
  return left.keys().map(function(key) {
      return [key, left(key).setUnion(right(key))]
    })
  .coerceTo('object')  
})   

Which produces:
{
    "1": [10, 19, 100],
    "2": [15, 100, 150]
}

