rethinkdb eqJoin匹配
rethinkdb eqJoin matching
您好,我正在使用 javascript 和 rethinkdb,我正在尝试弄清楚当目标 ID 不在主 table 上但在第二个上时如何使用 eqJoin table.
网站上的示例显示了这一点。
first table /players
[{name: 'anna', gameID:1}]
second table /games
[{id: 1, gameName: 'supergame'}]
但是我要加入的是这个
first table / players
[{id 1, name: 'anna'}]
second table / games
[{playerID: 1, gameName:'supergame'},
{playerID: 1, gameName:'megagame'},
{playerID: 1, gameName:'lamegame'}]
我已经阅读了一些关于 concatMap 的内容并尝试了这个,但这显然给我错误,还有其他方法吗?
如果有的话我还能加入更多 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)
右侧的 table 必须在您要获取的字段上有一个索引。从你的第二个例子来看,你已经在 games
上有一个索引 playerID
;如果是这种情况,那么您应该能够将 index: 'playerId'
指定为 eqJoin
的参数。类似于 r.table('players').eqJoin('id', r.table('games'), {index: 'playerId'})
.
如果该索引不存在,您可以使用indexCreate
创建它。
在 slack.rethinkdb.com 的帮助下,我得到了这个答案,效果很好。
这也将匹配的对象放在对象
上的树中
r.db('test').table("players").map(function(play){
return play.merge({ "games": r.db('test').table("games").getAll(trans("id"), {"index": "playerID"}).coerceTo("ARRAY")});
})
您好,我正在使用 javascript 和 rethinkdb,我正在尝试弄清楚当目标 ID 不在主 table 上但在第二个上时如何使用 eqJoin table.
网站上的示例显示了这一点。
first table /players
[{name: 'anna', gameID:1}]
second table /games
[{id: 1, gameName: 'supergame'}]
但是我要加入的是这个
first table / players
[{id 1, name: 'anna'}]
second table / games
[{playerID: 1, gameName:'supergame'},
{playerID: 1, gameName:'megagame'},
{playerID: 1, gameName:'lamegame'}]
我已经阅读了一些关于 concatMap 的内容并尝试了这个,但这显然给我错误,还有其他方法吗?
如果有的话我还能加入更多 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)
右侧的 table 必须在您要获取的字段上有一个索引。从你的第二个例子来看,你已经在 games
上有一个索引 playerID
;如果是这种情况,那么您应该能够将 index: 'playerId'
指定为 eqJoin
的参数。类似于 r.table('players').eqJoin('id', r.table('games'), {index: 'playerId'})
.
如果该索引不存在,您可以使用indexCreate
创建它。
在 slack.rethinkdb.com 的帮助下,我得到了这个答案,效果很好。 这也将匹配的对象放在对象
上的树中r.db('test').table("players").map(function(play){
return play.merge({ "games": r.db('test').table("games").getAll(trans("id"), {"index": "playerID"}).coerceTo("ARRAY")});
})