Question

I'm new to rethinkdb and have simple trouble. Suppose I have following objects structure:

Table A:
[{
    'id': '1',
    'b_list': ['11', '12']
}]
Table B:
[{
    'id': '11',
    'value': 'somevalue1'
},{
    'id': '12',
    'value': 'somevalue2'
}]

So instance of "A" keeps relations to two "B" instances.

What query should I do in rethinkdb to retrieve following response?

[{
    'id': '1',
    'b_list': [
        {
            'id': '11',
            'value': 'somevalue1'
        },{
            'id': '12',
            'value': 'somevalue2'
        }
    ]
}]

I want to see instances of B instead of their ids in response and I don't want this change to be saved.

Was it helpful?

Solution

The best way to do this is with an eq_join like so:

r.table("A")
  .map(lambda x: 
    x.merge({
      "b_list" :
        x["b_list"].eq_join(lambda x: x, r.table("B"))["right"]})

For a single document:

r.table("A").get(pk)
  .do(lambda x: 
    x.merge({
      "b_list" :
        x["b_list"].eq_join(lambda x: x, r.table("B"))["right"]})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top