0

Hi I'm using javascript with rethinkdb, and I'm trying to figure out how to use eqJoin when the id of the target is not on the maintable but on the second table.

The examples on the website shows this.

first table /players
[{name: 'anna', gameID:1}]

second table /games
[{id: 1, gameName: 'supergame'}]

But what I want to join is this

first table / players
[{id 1, name: 'anna'}]

second table / games
[{playerID: 1, gameName:'supergame'},
 {playerID: 1, gameName:'megagame'},
 {playerID: 1, gameName:'lamegame'}]

I have read a little about concatMap and tried this but that obviously gives me errors, is there any other way?

And if there is could I even join even more tables?

r.table("players").concatMap(function(play) {
    return r.table("games").getAll(
        play("id"),
        { index:"playerID" }
    ).map(function(games) {
        return { left: play, right: games }
    })
}).run(conn, callback)
Stellan
  • 183
  • 11

2 Answers2

1

The table on the right has to have an index on the field you're getting. It looks from your second example like you have an index playerID on games already; if that's the case, then you should be able to just specify index: 'playerId' as an argument to eqJoin. Something like r.table('players').eqJoin('id', r.table('games'), {index: 'playerId'}).

If that index doesn't exist, you can use indexCreate to create it.

mlucy
  • 5,249
  • 1
  • 17
  • 21
  • Thank you! the only thing missing though is that there is one player created for every game, but I would like the games to be attached to the single player it matches, is that possible? – Stellan Jan 14 '16 at 19:57
0

by some help from slack.rethinkdb.com I got this answer which works great. this also puts the matching objects in a tree on the object

r.db('test').table("players").map(function(play){ 
    return play.merge({ "games": r.db('test').table("games").getAll(trans("id"), {"index": "playerID"}).coerceTo("ARRAY")}); 
})
Stellan
  • 183
  • 11