0

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.

Stqs
  • 355
  • 1
  • 4
  • 13

1 Answers1

1

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"]})
Joe Doliner
  • 2,058
  • 2
  • 15
  • 19
  • yeah, you are right, that's it. that works fine if I need to fetch all data from table A this way. but what if I need this only for one record, with id = "1" there is no need to use map anymore but we still need merge r.table("A").get("1").merge({'b_list': XXX["b_list"].eq_join(lambda bla bla bla)}) what should I put instead of XXX there to access doc I'm currently merging. is there something like 'self' in queries? – Stqs Dec 08 '13 at 22:40
  • If it's only one document then you want to use `do` instead of `map`, I'm going to edit the answer to include it. – Joe Doliner Dec 10 '13 at 21:23