如何在RethinkDB中加入三个或更多表?

时间:2015-11-16 19:52:39

标签: rethinkdb

我有三个表:userpostpostVote

我想将这些表连接在一起,以构建当前会话用户的帖子列表。结果列表中每个帖子项的vote字段将根据当前会话用户和每个帖子项目的不同而不同。想想reddit投票是如何运作的。

我在['userId', 'postId']表中添加了userIdpostId的{​​{1}}复合索引,速度更快postVote

以下是表格模式:

eqJoin

期望的结果:

user {
  id: string
  name: string
}

post {
  id: string
  authorId: string
  title: string
}

postVote {
  id: string
  userId: string
  postId: string
  vote: number
}

1 个答案:

答案 0 :(得分:3)

我认为这可能会成功。

Array
(
    [74] => 'value74'
    [75] => 'value75'
    [76] => 'value76'
)

最后,您应该在最顶层有一个r.table("postVotes") .eqJoin("authorId", r.table("users")) .eqJoin(r.row("left")("postId"), r.table("posts")) .map(function(doc){ //...clean up doc as necessary... }).run() left键的文档,然后另一个rightleft包含在最顶层right值。left最后执行.map将允许您将其清理为所需的JSON结果。

这是一个近似值,因为我没有您的数据集,但如果您有任何问题,请与我联系。

相关问题