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)
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.