non writing replace/update in rethinkdb query

88 views Asked by At

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.

1

There are 1 answers

2
Joe Doliner On BEST ANSWER

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"]})